Of PHP versions and strtotime

So Jason and I stumbled upon an ugly version dependency. It wasn’t terrible but required special testing and fixing and was annoying for a few minutes.

Jason has PHP version 5.2.6 on his machine. He wrote a php page for me that did some date math using strtotime. Specifically he did this:


$earliest_time = strtotime(
     $schedule_items[0]['week'].' +'.$_GET['shiftweeks'].' week');

Nothing exciting here, the week from schedule items is incremented by a number of weeks on the url.

The interesting thing is that the value of shiftweeks can be negative. So now you end up with a string that looks like

2009-03-06 +-2 week

PHP 5.2.6 was happy enough with this.

Then I tried it on my box, which has version 5.2.0. No can do. PHP quietly failed giving a timestamp of 0. Took a few minutes to figure out what was happening. The fix wasn’t hard:


    if ($_GET['shiftweeks'] < 0)
        $op = "";
    else
        $op = "+";
    $earliest_time = strtotime(
        $schedule_items[0]['week'].' '.$op.$_GET['shiftweeks'].' week');

Leave a Reply