Here are some little tricks that might be useful to you if you're working with dates and times in PHP:
- A "Unix timestamp" or "epoch" refers to the number of seconds since 1970-01-01 00:00:00 UTC. (i.e. a point in time in one specific timezone.) A Unix timestamp is never timezone-specific; if you call
time()at the same moment on computers in different time zones, you get exactly the same value back.
gmmktime- Passed a number of arguments (day, month, year, etc.), returns a Unix timestamp.
mktimeinterprets its arguments as representing a local time, whilst
gmmktimeinterprets its arguments as a time in the time zone UTC. (That is, given the same input
gmmktimereturns the same results everywhere;
mktimereturns timezone-specific results.) Note that in this case, the
gm-prefix affects how the input is interpreted.
gmstrftime- Passed a Unix timestamp and format string, and returns a string. In
strftime's case, the string is the time according to the current timezone, whilst
gmstrftimereturns the time in the UTC timezone. (Given the same input,
gmstrftimereturns the same time in all timezones (though not necessarily the same string--the actual string may be different if the locale is different) whilst
strftimereturns timezone-specific results.) Note that in this case, the
gm-prefix affects the output string.
strptime- Inverse of
gmstrftime: passed a time string and a format string, and returns an array representing the local time. (This not a great return value; it would have been better to return a Unix timestamp.) The time string is interpreted as a local time; there is no
gm-equivalent, or even any way to simulate one, since it ignores the value of
date_default_timezone_set. Note that you can impose a timezone on the input string if it contains a timezone abbreviation or offset (e.g.
Sun 26 Apr 2009 21:50:35 BST) which the format string reads with
gmdate- Passed a Unix timestamp and a format string, returns a string. As with
gm-prefix affects whether the result is a representation of the time in the current timezone or in the UTC timezone. Note that the format string is completely different to that of
gmstrftime! (One reason to use this set of functions is for the useful date format constants.) There is no inverse of this function.
strtotime- Passed a string in "US English date format", and returns a Unix timestamp. Note that there is no way to override the US date format!
strtotime ("03/04/2008")is the 4th of March 2008. Note that this function will parse relative times, like
next monday; this is about the only reason to use it.
- How does PHP know what your current timezone is? It tries a few different places, including the
TZenvironment variable and the
date.timezoneini option. (See
date_default_timezone_getfor more information.)