strtotime 函数传递错误的日期和时间格式的奇怪行为
Strange behavior with the strtotime function passing a wrong date and time format
我正在尝试了解日期函数的以下行为 strtotime()
:
strtotime(1420066800)
returnsfalse
,符合预期。
strtotime(1451602800)
returns26197048320
!但预期 false
因为 1451602800
不是有效的日期和时间格式。 (new DateTime(1451602800)
也一样)
我阅读了整个 Supported Date and Time Formats 页面,但没有找到与此输入匹配的内容。
顺便说一句,这些是 2015-01-01
和 2016-01-01
的相对时间戳。
任何见解将不胜感激。
更新:
不同版本测试结果:
PHP 5.1.6 => false
PHP 5.2.17 => 26197048320
PHP 5.3.29 => 26197048320
PHP 5.4.34 => 26197048320
PHP 5.5.18 => 26197048320
PHP 5.6.2 => 26197048320
PHP 5.6.17 => 26197048320
PHP 7.0.2 => 26197048320
运行 Mac OS X 10.11.3
让我们从正确的方法开始吧。如果你想将时间戳传递给 strtotime
,你必须在它前面加上 '@'
。它在 Compound Formats 页面的 "Localized notations" -> "Unix Timestamp".
下进行了解释
echo(date('r', strtotime('@1420066800'))."\n");
echo(date('r', strtotime('@1451602800'))."\n");
输出为:
Thu, 01 Jan 2015 01:00:00 +0200
Fri, 01 Jan 2016 01:00:00 +0200
现在,为什么 strtotime()
returns false
for 1420066800
and 26197048320
for 1451602800
?
它期望接收到一个字符串,如果它接收到一个它不关心的数字并首先将其转换为字符串。然后它遵循一些规则并尝试将日期组件识别到字符串中。因为 '1420066800'
和 '1451602800'
都不包含任何组件分隔符,它可能会尝试猜测组件的顺序。
今天,2016-02-25
,strtotime('1451602800')
生成一个时间戳,转换为可打印日期如下所示:'Fri, 25 Feb 2800 14:52:00 +0200'
这让我觉得它解释输入字符串如下:14:51:60
是时间,2800
是年份,其他组件(日、月)从当前时间开始初始化。
文档说:
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC
), relative to the timestamp given in $now
, or the current time if $now
is not supplied.
由于您提供的 "date" 不遵循任何有效的日期时间格式,strtotime()
可以自由 return 任何值。它被称为"garbage in, garbage out".
1451602800
给出时间戳2800-02-25 14:52:00
;并被解释为:
1451602800
------
^
14:51:60 (or 14:52) time
今年的今天
1451602800
----
^
2800
但仅限于可以处理该日期范围的 64 位系统
我正在尝试了解日期函数的以下行为 strtotime()
:
strtotime(1420066800)
returnsfalse
,符合预期。strtotime(1451602800)
returns26197048320
!但预期false
因为1451602800
不是有效的日期和时间格式。 (new DateTime(1451602800)
也一样)
我阅读了整个 Supported Date and Time Formats 页面,但没有找到与此输入匹配的内容。
顺便说一句,这些是 2015-01-01
和 2016-01-01
的相对时间戳。
任何见解将不胜感激。
更新:
不同版本测试结果:
PHP 5.1.6 => false
PHP 5.2.17 => 26197048320
PHP 5.3.29 => 26197048320
PHP 5.4.34 => 26197048320
PHP 5.5.18 => 26197048320
PHP 5.6.2 => 26197048320
PHP 5.6.17 => 26197048320
PHP 7.0.2 => 26197048320
运行 Mac OS X 10.11.3
让我们从正确的方法开始吧。如果你想将时间戳传递给 strtotime
,你必须在它前面加上 '@'
。它在 Compound Formats 页面的 "Localized notations" -> "Unix Timestamp".
echo(date('r', strtotime('@1420066800'))."\n");
echo(date('r', strtotime('@1451602800'))."\n");
输出为:
Thu, 01 Jan 2015 01:00:00 +0200
Fri, 01 Jan 2016 01:00:00 +0200
现在,为什么 strtotime()
returns false
for 1420066800
and 26197048320
for 1451602800
?
它期望接收到一个字符串,如果它接收到一个它不关心的数字并首先将其转换为字符串。然后它遵循一些规则并尝试将日期组件识别到字符串中。因为 '1420066800'
和 '1451602800'
都不包含任何组件分隔符,它可能会尝试猜测组件的顺序。
今天,2016-02-25
,strtotime('1451602800')
生成一个时间戳,转换为可打印日期如下所示:'Fri, 25 Feb 2800 14:52:00 +0200'
这让我觉得它解释输入字符串如下:14:51:60
是时间,2800
是年份,其他组件(日、月)从当前时间开始初始化。
文档说:
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since
January 1 1970 00:00:00 UTC
), relative to the timestamp given in$now
, or the current time if$now
is not supplied.
由于您提供的 "date" 不遵循任何有效的日期时间格式,strtotime()
可以自由 return 任何值。它被称为"garbage in, garbage out".
1451602800
给出时间戳2800-02-25 14:52:00
;并被解释为:
1451602800
------
^
14:51:60 (or 14:52) time
今年的今天
1451602800
----
^
2800
但仅限于可以处理该日期范围的 64 位系统