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="http://us3.php.net/manual/en/function.date.php">date()</a>, <a href="http://us3.php.net/manual/en/function.strtotime.php">strtotime()</a>, and <a href="http://us3.php.net/manual/en/function.mktime.php">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.lang="php">date_default_timezone_set('GMT');

$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 <a href="http://us3.php.net/manual/en/function.timezone-open.php">DateTimeZone</a> 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 <a href="http://us3.php.net/manual/en/function.date-timezone-set.php">setTimezone()</a> 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 <a href="http://us3.php.net/manual/en/function.date-format.php">format</a> 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...lang="php"> <?php

if ( $twitter_xml = twitter_status('12345678') ) {


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

$i = 0;

foreach ($twitter_xml->status as $key => $status) {

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


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

?> * <?php echo $status->text . ’ ’ . $time_display; ?>



  if ($i == 5) break;


?> * more…


} else {

echo 'Sorry, Twitter seems to be unavailable at the moment...again...';



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**
* <a href="/2008/11/display-twitter-updates-on-your-website/">Display Twitter updates on your website</a>
<li class="green bold">Calculate dates and times in different timezones (translate Twitter timestamps)
* <a href="/2008/12/parse-urls-in-text-create-links">Parse URL&rsquo;s in text, create links</a>