如何从 Unix 时间戳 (PHP) 中获取星期几?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2475550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the day of the week from a Unix timestamp (PHP)?
提问by horgen
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).
如何从 PHP 中的 Unix 时间戳获取日期(1-7)?我还需要日期(1-31)和月份(1-12)。
回答by marvin
回答by VolkerK
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";
回答by Dyllon
It's the date()function you're after.
这是您所追求的date()函数。
You can get more details from the PHP manual but in a nutshell here are the functions you need.
您可以从 PHP 手册中获得更多详细信息,但简而言之,这里是您需要的功能。
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
回答by muruga
print "Week".date('N')."\n";
print "day of month " .date('d')."\n";
print "month ".date('m')."\n";
回答by dyve
Use the datefunction as stated before, with your $timestampas the second argument:
使用前面所述的日期函数,将您的$timestamp作为第二个参数:
$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 DateTimeobject.
并非所有 PHP 版本都可以使用负时间戳。我的经验是,使用新的DateTime对象可以更好地追溯到 UNIX 时代之前的时间戳。

