关于strtotime(),我发现了一些奇怪的东西.
在不存在的日期,它返回第二天.
$d30= strtotime("2017-06-30");
Echo $d30 ."\n";
Echo date("Y-m-d", $d30)."\n\n"; // 2017-06-30
$d31= strtotime("2017-06-31");
Echo $d31 ."\n";
Echo date("Y-m-d", $d31)."\n\n"; // 2017-07-01
$d32= strtotime("2017-06-32");
Echo $d32 ."\n";
Echo date("Y-m-d", $d32); // 1970-01-01
我理解最后一个.它没有返回任何内容,因为它是一个错误.
但为什么第二个会在7月1日之前回归?
它是否意味着功能性,如果你犯了错误,它会“纠正你”?或者它是strtotime()中的真正错误?
解决方法:
如果您查看strtotime()的文档,您将看到第一个参数是:
time
A date/time string. Valid formats are explained in 07001.
如果您按照日期和时间格式的链接进行操作,请转到Date Formats,您会看到:
因此,对于日期格式(即,DD),01-31是有效的(因为3只能跟随0或1),尽管是月份.根据提供的月份和日期值,将调整日期.
也可以在同一页面上的说明中找到:
Note:
It is possible to over- and underflow the dd and DD format. Day 0 means the last day of previous month, whereas overflows count into the next month. This makes “2008-08-00” equivalent to “2008-07-31” and “2008-06-31” equivalent to “2008-07-01” (June only has 30 days).07004
因此06-31有效而06-32无效.
Note: the day (dd or DD) is first checked for range 0..31 and only if it fits, the overflow and underflow mechanism may apply. If not, strtotime() simply returns false.
If you need unlimited over/underflow for date calculations (for example 2015-01-40 to 2015-02-09), use 07007 instead.07008
标签:php,strtotime
来源: https://codeday.me/bug/20190722/1501311.html