Stefan Fischerländer’s Blog One Blog Is Not Enough

Different strtotime() behaviour in PHP 4 and PHP 5

I just wasted about an hour of my working time looking for an error in my code. I tried to use the built-in strtotime() function to convert a date string from a log file to the appropriate timestamp:

The date string looks like this: 1/Jan/2007:08:03:50 +0100
Using phpa, the command line PHP tool, I got what I expected:
PHP 5.2.0 (cli) (???) [Darwin]
>>> print strtotime("1/Jan/2007:08:03:50 +0100");
1167635030

But in my PHP script, the very same command returns -1. After a lot of experiments, I found the problem. While my phpa tool uses PHP5, the script is invoked via php - which defaults to PHP 4.4.7. Obviously, the date format used in log files was added to strtotime() in PHP5:
macbook:~ sf$ php -r 'print phpversion()."\n"; print strtotime("01/Jan/2007:08:03:50 +0100")."\n";'
4.4.7
-1
macbook:~ sf$ php5 -r 'print phpversion()."\n"; print strtotime("01/Jan/2007:08:03:50 +0100")."\n";'
5.2.0
1167635030

So, be sure to check what PHP version you’re running, if you get into trouble with strtotime().

3 Responses to “Different strtotime() behaviour in PHP 4 and PHP 5”

  1. coco says

    Echt gute Seite, macht super Spass hier zu lesen. Interessante Artikel, die gut geschrieben sind. Hoffe bald mehr von Euch;-)

  2. werner says

    Thanks for your instruction, it helps me very well.

  3. DITA says

    Ich finde die Seite echt klasse ^.^

Leave a Reply