PHP – Notes on Time / Time Zones / Date

Just a few notes for any PHP coders, that I dont think makes clear enough.

time() is always GMT, seconds from 1970 GMT to the current time GMT, not to the local server time.
This can also be shown with: time() == date("U") == gmdate("U")

Its the date() function that makes GMT seconds into local server time, hence the need for gmdate().

Then you have strtotime() which returns seconds like time() but changes depending on the servers time zone.

$time = strtotime("2009-04-04"); echo gmdate("Y-m-d H:i:s", $time); outputs 2009-04-03 23:00:00
Why the loss of an hour ? Because it was run on a server with Day Light Savings that apply in April.
Instead you have to be more specific and use strtotime("2009-04-04 GMT")

However that wont help when doing certain date calculations:

$time = strtotime("1993-04-04 GMT");
echo gmdate("Y-m-d H:i:s", $time); outputs 1993-04-04 00:00:00
echo strtotime("+1 day GMT", $time); outputs 1993-04-04 23:00:00

Here you can instead add the seconds in a day ( $time += 60 * 60 * 24 ) but thats not suitable for adding months.

From PHP 5.1.0 there is the option date_default_timezone_set() however PHP4 is still widely used.

Oh and Happy New Year 🙂