错误日期的 strtotime
strtotime on incorrect dates
我发现 strtotime() 有一些奇怪的地方。
在不存在的日期 return 后一天。
$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
最后一句我明白了。它 return 没什么,因为它是一个错误。
但是为什么第二个 return 是七月的第一天?
它是否意味着功能,以防万一你犯了错误它会 "correct you"?还是 strtotime() 中的真正错误?
一个月中的第 31
天是可能的 strtotime()
将为您更正日期。如果您在 2 月 (2017-02-31) 尝试它,它将更正为 2017-03-03
。这就是您找到的。
所以,它的本质作用是:
- 获取日期
- 检查日期是否在有效范围内(每天数
月)
- 如果无效则计算下个月的天数
此行为在 strtotime
函数本身中实现。
文档页面上对此有很好的评论,但我找不到了。此 comment 提供了一些额外信息(请务必检查评论中的 link)。
如果您查看 strtotime() 的文档,您会看到第一个参数是:
time
A date/time string. Valid formats are explained in Date and Time Formats.
如果您遵循 link 的日期和时间格式并转到 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).1
因此06-31有效,而06-32无效。
此外,在 User Contributed Notes section, the note by Mirek at 2015-04-01 01:14 中可能是 useful/interesting:
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 mktime() instead.2
1http://php.net/manual/en/datetime.formats.date.php
2http://php.net/manual/en/datetime.formats.date.php#Hcom117014
我发现 strtotime() 有一些奇怪的地方。
在不存在的日期 return 后一天。
$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
最后一句我明白了。它 return 没什么,因为它是一个错误。
但是为什么第二个 return 是七月的第一天?
它是否意味着功能,以防万一你犯了错误它会 "correct you"?还是 strtotime() 中的真正错误?
一个月中的第 31
天是可能的 strtotime()
将为您更正日期。如果您在 2 月 (2017-02-31) 尝试它,它将更正为 2017-03-03
。这就是您找到的。
所以,它的本质作用是:
- 获取日期
- 检查日期是否在有效范围内(每天数 月)
- 如果无效则计算下个月的天数
此行为在 strtotime
函数本身中实现。
文档页面上对此有很好的评论,但我找不到了。此 comment 提供了一些额外信息(请务必检查评论中的 link)。
如果您查看 strtotime() 的文档,您会看到第一个参数是:
time
A date/time string. Valid formats are explained in Date and Time Formats.
如果您遵循 link 的日期和时间格式并转到 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).1
因此06-31有效,而06-32无效。
此外,在 User Contributed Notes section, the note by Mirek at 2015-04-01 01:14 中可能是 useful/interesting:
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 mktime() instead.2
1http://php.net/manual/en/datetime.formats.date.php
2http://php.net/manual/en/datetime.formats.date.php#Hcom117014