我有这个代码:
$endDate='02-20-2014'; //Just updated this
echo date('Y-d-m',strtotime($endDate));
为什么
strtotime()
一直给我1969-31-12
?我的目标是获得:
2014-02-20
最佳答案
根本问题
PHP supported date formats不包括您正在使用的输入日期格式,'02-20-2014'
或 mm-DD-YY
为什么你会收到 1969-31-12
由于您的输入格式错误,strtotime()
失败。在 PHP 5.1 及更高版本中,strtotime()
会返回 false
在这种情况下和 date()
调用将评估为 1970-01-01
(即 the day of the UNIX epoch )。
但是,在您的情况下 strtotime()
好像回来了-1
相反,失败。这表明您使用的是过时版本的 PHP ( 5.0.x or less )。它还会导致 date()
调用返回 1969-31-12
(即减去一秒 the UNIX epoch )
解决方案
将您的输入日期格式更改为任何 PHP supported date formats .另外,为了得到你指定的输出日期格式,2014-02-20
或 YY-mm-dd
,您应该调整 format
date()
的论据功能符合 Y-d-m
至 Y-m-d
.
例如:
$endDate='2014-20-02';
echo date('Y-m-d',strtotime($endDate));
关于 PHP 版本的旁注
如果可能,强烈建议您将系统从过时的 PHP 5.0.x 版本迁移到最新版本。你应该小心检查the change logs of each version migration ever since以确保您没有破坏任何现有代码。
关于php - 为什么 strtotime() 和 date() 返回 1969-31-12?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22260796/