PHP strtotime in 64 bit environment

By | October 23, 2011

The strtotime function is used to convert a date in various formats to a timestamp. However its behaviour is different based on architecture it is running on, whether 32bit or 64bit.

Lets take a few examples :

In 64 bit environment

desktop:~$ php -a
Interactive shell

php > echo strtotime("0000-00-00 00:00:00");
-62170005200
php > echo strtotime('1000-01-30');
-30607739600
php > echo strtotime('2100-01-30');
4104930600
php >

Over here we can see that strtotime is producing timestamps for far dates like 30-01-2100 and 30-01-1000 and even 00-00-0000

whereas on a 32 bit machine it would do this :

echo var_dump(strtotime("0000-00-00 00:00:00"));

echo var_dump(strtotime('1000-01-30'));

echo var_dump(strtotime('2100-01-30'));

Output :

bool(false)
bool(false)
bool(false)

So strtotime is not able to handle far away dates on a 32bit machine.

This can have effects in your code. For example if you are checking a date to be valid based on whether strtotime returns false or not :

if(! strtotime($date))
{
//error
}

Then the above approach would fail when the machine is 64 bit, because strtotime would simply succeed. On 64 bit , the date range for strtotime is quite large compared to that on 32 bit. Therefore an indepth checking of date range is necessary on the more powerful 64bit architecture.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

Leave a Reply

Your email address will not be published. Required fields are marked *