PHP Datetime cannot convert negative date to ISO8601

Converting a negative date using DateTime returns 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)

PS A negative date string was created using the format $ d-> (\ DateTime :: ISO8601);

PHP version of PHP 5.4.28

+4
source share
1 answer

According to Format manual

  public string DateTime::format ( string $format ) 

Where

 $format Format accepted by date(). 

and if you look at date ()

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

it should be

 $d->format("c") 

because "c" matches the format in date ()

Date ISO 8601 (added in PHP 5)

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

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

while

 $d->format("c"); 

exits

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

Hope this helps.

0
source

All Articles