A friend of mine who is rather new to PHP was getting super confused about timestamps and timezones. So I thought I'd post a quick thought about timestamps today to clear up some confusion.
As you know, timestamps represent the number of seconds elapsed since January 1, 1970 00:00:00 UTC. The UTC timezone there is important. Basically all unix timestamps are in UTC -- there is no "EST timestamp" for example, there is only UTC.
Let's be clear about that: If we both compared our timestamps on our computers right now, regardless of where in the world we were, the timestamps would match. A timestamp is a timestamp is a timestamp (how's that for poetic geekery :-).
But confusion ensues when you want to create timestamps. For example, using PHP's mktime() function. The problem is that to specify a time you have to know what timezone it is. Imagine if I were to say, "It is 19:00". That statement would make no sense; it's ambiguous. To make any sense at all, I need to specify where it is 19:00 -- and our world does this via timezones.
The thing to keep in mind is that "human time" is different in every timezone. For humans, my 19:00 might be your 16:00, which might be someone else's 22:00. But in "computer time" of timestamps, all of these times are the same -- they are all timestamps. Because a point in time is the same point in time everywhere, only when we "render" time to "human time" do we introduce the concept of reference points (timezones).
How do I make a timestamp that is my 19:00? To do this, all I need to do is create a timestamp that is 19:00 UTC, and then calculate my timezone offset. Why does this work? Remember that 19:00 is the exact same point in time everywhere in the world -- the only thing that changes is how we humans reference that time. Thus, "my" 19:00 is not UTC 19:00. To get "my" 19:00, I get UTC's 19:00 and offset it for my timezone, so that the timestamp correctly represents 19:00 from my point of view.
Timezones with PHP
The point of this post was originally to talk about confusions with PHP. Because it seems a little known fact that PHP's mktime() converts input from local time into UTC. That is, it does the offset calculations for you.
So when I do this with PHP (create a time where it is 19:00):
PHP uses your systems timezone and offsets the time for you. If the systems timezone is GMT-5, then it'll offset 5 hours to create the correct UTC representation of that time. (PHP will use the system defined timezone for your server unless you use date_default_timezone_set)
What about when we do these:
These mean "the current timestamp". As we discussed before, a point in time is the exact same no matter where you are in the world -- so the timestamp for "right now" requires no offset calculations (there is no ambiguity for "right now"). Timezones only matter when you're referencing a date and time specifically (aka, using mktime() with arguments).
If you want to make a timestamp with time elements that are already in UTC time, then you should use the gmmktime() function instead. Alternatively, you could set PHP's internal timezone to UTC.
Leave a Reply