[英]ISO-8601 numeric representation of the day of the week (“N”) in PHP using the date() function always returns 3
Trying to get the ISO-8601 numeric representation of the day of the week ("N") in PHP using the date() function; 试图使用date()函数获取PHP中星期几(“ N”)的ISO-8601数字表示形式; however, it keeps returning "3" no matter what day I use with mktime().
但是,无论我在mktime()中使用哪一天,它始终返回“ 3”。
<?php
$date = date( "Y-m-d H:i:s", mktime(0, 0, 0, 9, 16, 2011) );
//$date = date( "Y-m-d H:i:s", mktime(0, 0, 0, 9, 17, 2011) );
print_r(date('N', $date));
?>
Output: 3 输出3
You shouldn't feed a date string into the second argument for date()
, it should be an integer containing the Unix timestamp (the value returned from mktime()
). 您不应该将日期字符串输入
date()
的第二个参数中,它应该是包含Unix时间戳(从mktime()
返回的值mktime()
的整数。 See the date()
documentation . 请参阅
date()
文档 。
$date = mktime(0, 0, 0, 9, 16, 2011);
var_dump(date('N', $date)); // string(1) "5"
With your original code: 使用您的原始代码:
$date = date( "Y-m-d H:i:s", mktime(0, 0, 0, 9, 16, 2011) );
print_r(date('N', $date));
The value of $date
is "2011-09-16 00:00:00"
. $date
值为"2011-09-16 00:00:00"
。 This is not an integer, and certainly not the Unix timestamp for that date/time; 这不是整数,当然也不是该日期/时间的Unix时间戳。 because of that,
date()
cannot work with the value and reverts back to using the Unix epoch ( 0
timestamp) which is 1 Jan 1970. Also, an E_NOTICE
message stating " A non well formed numeric value encountered in [file] on line [line] " is issued. 因此,
date()
无法使用该值,并恢复使用1970年1月1日的Unix纪元( 0
时间戳)。此外,一条E_NOTICE
消息指出“ 在线[文件]中遇到格式错误的数值[行]发出。
PHP is trying to interpret the string generated by date( "Ymd H:i:s", mktime(0, 0, 0, 9, 16, 2011) );
PHP正在尝试解释由
date( "Ymd H:i:s", mktime(0, 0, 0, 9, 16, 2011) );
生成的字符串date( "Ymd H:i:s", mktime(0, 0, 0, 9, 16, 2011) );
as a date, but PHP uses seconds since epoch as a datatime. 作为日期,但是PHP使用从纪元起的秒作为数据时间。 You can just pass the result of mktime into the second data function call like this:
您可以像这样将mktime的结果传递给第二个数据函数调用:
$dateTime = mktime(0, 0, 0, 9, 16, 2011)
$date = date("Y-m-d H:i:s", $dateTime);
echo date('N', $dateTime);
// results in "5"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.