Hot Koehls

The more you know, the more you don’t know

This content is a little crusty, having been with me through 3 separate platform changes. Formatting may be rough, and I am slightly less stupid today than when I wrote it.
25 Nov 2008

Easily calculate dates and times in different timezones

Building off my last post, where I showed you how to easily display any public Twitter feed on your site, I ran into another problem: the dates that are delivered by the Twitter API all reflect Greenwich Mean Time (GMT). Now I thought about going the old route and doing some convoluted math using <a href="">date()</a>, <a href="">strtotime()</a>, and <a href="">mktime()</a>, but I thought it was time find a serious solution, one that would allow me to display the correct time for whatever timezone I wished. So while searching the web, I came across an insightful post that discussed PHP’s built-in DateTime object. I try hard to keep up with all the tech in my job, but this one got through the cracks, and I was instantly intrigued. So after some research and fiddling, I came up with the following block of code, which will allow you to easily translate dates/times to and from any timezone.


$datetime = new DateTime('2008-11-25 16:48:13');

$tz = new DateTimeZone('America/New_York');


$time_display = $datetime->format('D, M jS g:ia T');

The process is a little convoluted, so here's what's going on. First we must set the system-wide timezone to match that of the date/time we would like to translate. In this example, it's about 4:50pm on Nov 25, 2008 in the GMT timezone. Next we create a DateTime object with that time (FYI you can use any format accepted by strtotime()`).`
Next we create a `DateTimeZone` object. Keep in mind that this step stands completely on its own, and can be performed anytime up to now.
Finally, we pass the `DateTimeZone` object as the lone argument in a call to the `setTimezone()` method. This will change the timezone stored in $datetime from GMT to EST, and automatically update the date/time accordingly, which we output with a call to the `format` method.
Note that I used object-oriented syntax for this process, but there is a procedural style (i.e. functions) as well. I prefer OOP here because it's just easier to read and follow.
Valid arguments for `date_default_timezone_set()` and `DateTimeZone()` come from PHP's list of supported timezones.
So now that we can translate timezones, let's apply it to my Twitter example...
status as $key => $status) {

      $datetime = new DateTime($status->created_at);


      $time_display = $datetime->format('D, M jS g:ia T');

* text . '
' . $time_display; ?>

* more...


Sidenote: While I was working on this I found GMT and UTC used almost interchangeably. For all intents and purposes, they are identical. But there is a technical difference: GMT is based on the rotation of the earth (less precise), while UTC is based on an atomic clock (more precise). Don't worry, you and I are not the only ones who got confused.
**Build a slick Twitter feed on your site**
    * Display Twitter updates on your website
  1. Calculate dates and times in different timezones (translate Twitter timestamps) * Parse URL’s in text, create links

comments powered by Disqus