How to get the day of the week from a Unix timestamp (PHP)?

 ̄綄美尐妖づ 提交于 2019-11-30 02:48:53

问题


How do I get the day (1-7) from a Unix timestamp in PHP? I also need the day date (1-31) and month (1-12).


回答1:


You can use date() function

$weekday = date('N', $timestamp); // 1-7
$month = date('m', $timestamp); // 1-12
$day = date('d', $timestamp); // 1-31



回答2:


see http://docs.php.net/getdate

e.g.

$ts = time(); // could be any timestamp
$d=getdate($ts);

echo 'day of the week: ', $d['wday'], "\n";
echo 'day of the month: ', $d['mday'], "\n";
echo 'month: ', $d['mon'], "\n";



回答3:


It's the date() function you're after.

You can get more details from the PHP manual but in a nutshell here are the functions you need.

date('N', $timestamp);
//numeric representation of the day of the week

date('j', $timestamp);
//Day of the month without leading zeros

date('n', $timestamp);
//Numeric representation of a month, without leading zeros



回答4:


print "Week".date('N')."\n";
print "day of month " .date('d')."\n";
print "month ".date('m')."\n";



回答5:


Use the date function as stated before, with your $timestamp as the second argument:

$weekday = date('N', $timestamp); // 1 = Monday to 7 = Sunday
$month = date('m', $timestamp); // 1-12 = Jan-Dec
$day = date('d', $timestamp); // 1-31, day of the month

Not all PHP versions play nice with negative timestamps. My experience is that timestamps dating back to before the UNIX epoch fare better with the new DateTime object.



来源:https://stackoverflow.com/questions/2475550/how-to-get-the-day-of-the-week-from-a-unix-timestamp-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!