PHP Datetime fail to convert negative ISO8601 date

匿名 (未验证) 提交于 2019-12-03 03:03:02

问题:

Converting a negative date with DateTime get me false

Test code

var_dump(\DateTime::createFromFormat(\DateTime::ISO8601, '-0001-11-30T00:00:00+0100'));

Result

boolean false

Expected result

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)

(or something similar)

P.S. The negative date string was created with $d->format(\DateTime::ISO8601);

PHP version PHP 5.4.28

回答1:

According to manual of Format

 public string DateTime::format ( string $format )

where

$format  Format accepted by date().

and if you look in date()

$d->format(\DateTime::ISO8601);

should be

$d->format("c")

because "c" is according to format in date()

ISO 8601 date (added in PHP 5)

In my test $d->format(\DateTime::ISO8601); outputs

2015-09-15T00:00:00+0200

while

$d->format("c");

outputs

2015-09-15T00:00:00+02:00

I hope it helps.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!