Deep thoughts about timestamps and time zones
I’ve been struggling with Drupal’s timezone/daylight savings handling (see Drupal discussion).
In the course of trying to get things to work right, I gave the concept of timestamps a much closer look.
For starters, it really dawned on me that a timestamp, by definition, is NOT the number of seconds since January 1, 1970. It’s the number of seconds since January 1, 1970 UTC (which is also known as GMT).
In other words, if I were to create a timestamp here in New York that represents January 1, 1970 at 00:00:01 EST (one second after midnight), the time stamp would not be “1” as in one second - it would be: 18001. In other words, 60 seconds times 60 minutes * 5 hours (to account for the fact that today I am 5 hours behind UTC) + 1 second:
60 * 60 * 5 + 1 = 18001
PHP handles all of this conversion for us, which is part of the reason I’ve been so blissfully unaware of how complicated things are.
When I use php to create a timestamp, it creates a timestamp representing my date/time in UTC. When I use php to convert a timestamp to a more human readable form, it converts it back to my timezone:
0 jamie@liberace:~$ php -r 'echo mktime(0,0,1,1,1,1970) . "\n";'; 18001 0 jamie@liberace:~$ 0 jamie@liberace:~$ php -r 'echo date("Y-m-d H:i:s",18001) . "\n";'; 1970-01-01 00:00:01 0 jamie@liberace:~$
If you want to get the actual UTC timestamp for a date, use the gmmktime and gmdate functions instead.
Also of note - the world’s easiest way to convert a timestamp to a local, human-readable date:
date -d @18001