Michael Stillwell

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.
  • mktime/gmmktime - Passed a number of arguments (day, month, year, etc.), returns a Unix timestamp. mktime interprets its arguments as representing a local time, whilst gmmktime interprets its arguments as a time in the time zone UTC. (That is, given the same input gmmktime returns the same results everywhere; mktime returns timezone-specific results.) Note that in this case, the gm- prefix affects how the input is interpreted.
  • strftime/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 gmstrftime returns the time in the UTC timezone. (Given the same input, gmstrftime returns the same time in all timezones (though not necessarily the same string--the actual string may be different if the locale is different) whilst strftime returns timezone-specific results.) Note that in this case, the gm- prefix affects the output string.
  • strptime - Inverse of strftime/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 %z or %Z.
  • date/gmdate - Passed a Unix timestamp and a format string, returns a string. As with strftime/gmstrftime, the 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 strftime/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 tomorrow and 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 TZ environment variable and the date.timezone ini option. (See date_default_timezone_get for more information.)
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Plus

11 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. michael said

    A few quick followup points to my own post:

    1. If you're using PHP 5.3, you may want to consider the DataTime module, which has been made a lot more useful in 5.3.

    2. I think I'm a little unusual in my unhappiness with strtotime(); it's a pretty easy way to parse ISO 8601 dates without having to look up format strings, for example. But even with relative dates it can return unexpected values (see Derick Rethan's post on "next month"), so I still think it's prudent to respect its power!

  2. Harro said

    ehhm.. wouldn't you get different values if you call time() on machines in different timezones at the same time?

    hmm I guess the definition of "at the same time" is a bit fuzzy. At the same time for the specific server's timezone or for the person calling the functions on the servers? :D

  3. michael said

    @Harro: I mean ... if you have a server in the US, and one in Europe, and you call time() at the same moment in both locations, you'll get the same value back. If you don't do this at exactly the same moment, there might be a small difference between the servers, but the point is that you'll never get a difference equivalent to several hours.

  4. Neither mktime or gmmktime take an array, they take a pile of optional parameters.

  5. michael said

    @SeanJA Thanks, you're completely correct, "array" is the wrong word. I've fixed the text now.

Continuing the Discussion

  1. Ibuildings techPortal: Tips for PHP Date and Time Functions | Development Blog With Code Updates : Developercast.com linked to this post on January 26, 2010

    [...] this new post from the Ibuildings techPortal today Michael shares some handy tips for working the date and time [...]

  2. Ibuildings techPortal: Tips for PHP Date and Time Functions | Webs Developer linked to this post on January 26, 2010

    [...] this new post from the Ibuildings techPortal today Michael shares some handy tips for working the date and time [...]

  3. Tips for PHP Date and Time Functions http://bit.ly/7e689B #php #tips #phptips

  4. php-html.net linked to this post on February 11, 2010

    Tips for PHP Date and Time Functions...

    Here are some little tricks that might be useful to you if you’re working with dates and times in PHP....

  5. PHP Date and Time functions – Tips and Tricks linked to this post on February 16, 2010

    [...] to: iBuildings Share and [...]

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.