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
date(),
strtotime(), and
mktime(), 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.
date_default_timezone_set('GMT'); $datetime = new DateTime('2008-11-25 16:48:13'); $tz = new DateTimeZone('America/New_York'); $datetime->setTimezone($tz); $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…
<ul> <?php if ( $twitter_xml = twitter_status('12345678') ) { date_default_timezone_set('GMT'); $tz = new DateTimeZone('America/New_York'); $i = 0; foreach ($twitter_xml->status as $key => $status) { $datetime = new DateTime($status->created_at); $datetime->setTimezone($tz); $time_display = $datetime->format('D, M jS g:ia T'); ?> <li><?php echo $status->text . '<br />' . $time_display; ?></li> <?php ++$i; if ($i == 5) break; } ?> <li><a href="http://twitter.com/YOUR_PROFILE_HERE">more...</a></li> <?php } else { echo 'Sorry, Twitter seems to be unavailable at the moment...again...'; } ?> </ul>
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.