Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php 32bit date parsing for dates before 13Dec 1901

I was happily using strtotime() on my development machine to parse dates some of which are in the 1800s, with an extreme example in the 1500s.

But my development machine is 64 bit and the server is 32 bit. On a 32 bit machine any date before Fri, 13 Dec 1901 20:45:54 UTC is out of range (see notes on strtotime).

I am happy to write some custom code for this, but thought I would ask if any of the php inbuilt functions will manage this. The dates are in ISO 8601 format.

For what it is worth the server is centos 5.4 with php 5.2.10

like image 594
Jeremy French Avatar asked Nov 28 '25 01:11

Jeremy French


1 Answers

1901-12-12T05:00:00Z for example

I can't test it right now, but DateTime's constructor may be able to accept your format without needing createFromFormat. I can't really tell from the documentation.

Try

try {
    $date = new DateTime('1901-12-12T05:00:00Z');
} catch (Exception $e) {
    echo "Arggh! ".$e->getMessage();
    die();
}

echo $date->format('Y-m-d H:i:s');

and see what happens, paying special attention to whether it understands the Z properly (i.e. makes it a UTC time).

DateTime uses 64 bit numbers internally on any system, and has no range limitations.

As said, from 5.3 on, you will be able to use createFromFormat which can parse any date expressed by one of date()'s placeholders. This is the optimal way, because you force it to parse a specific pattern, instead of guessing it.

like image 158
Pekka Avatar answered Nov 30 '25 15:11

Pekka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!