问题
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.
来源:https://stackoverflow.com/questions/31827328/php-datetime-fail-to-convert-negative-iso8601-date