用 DateTime 转换负日期让我出错
测试代码
var_dump(\DateTime::createFromFormat(\DateTime::ISO8601, '-0001-11-30T00:00:00+0100'));
结果
boolean false
预期结果
object(DateTime)[5]
public 'date' => string '-0001-11-30 00:00:00' (length=20)
public 'timezone_type' => int 1
public 'timezone' => string '+01:00' (length=6)
(或类似的东西)
附言负日期字符串是用 $d->format(\DateTime::ISO8601);
创建的PHP 版本 PHP 5.4.28
最佳答案
根据Format的手册
public string DateTime::format ( string $format )
在哪里
$format
Format accepted by date().
如果你查看date()
$d->format(\DateTime::ISO8601);
应该是
$d->format("c")
因为“c”符合 date() 中的格式
ISO 8601 date (added in PHP 5)
在我的测试中 $d->format(\DateTime::ISO8601);
输出
2015-09-15T00:00:00+0200
同时
$d->format("c");
输出
2015-09-15T00:00:00+02:00
希望对你有帮助。
关于PHP Datetime 无法转换负 ISO8601 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31827328/