Different strtotime() behaviour in PHP 4 and PHP 5

Stefan - 31. März 2008

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");

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";'
macbook:~ sf$ php5 -r 'print phpversion()."n"; print strtotime("01/Jan/2007:08:03:50 +0100")."n";'

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

Abgelegt in: PHP

3 Kommentare:

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

Thanks for your instruction, it helps me very well.

Ich finde die Seite echt klasse ^.^

Schreibe einen Kommentar
benötigt (wird nicht angezeigt)