How to Convert Time Zones in PHP

In this example, we are converting a date/time from US Central to GMT.
1) Create two TIme Zone objects, one for each time zone

$original_time_zone = new DateTimeZone('America/Chicago');
$new_time_zone = new DateTimeZone('GMT');

2) Create a Date Time object with the original time zone and the time desired.

$date_time = new DateTime('5-1-2011 11:30 PM', $original_time_zone);
//another example using the current system time
$date_time = new DateTime('now', $original_time_zone);

3) Convert to the new time zone using Date Time Object’s setTimeZone method.

$date_time->setTimezone($new_time_zone); //GMT time zone object

4) That’s it! You can also output the converted date time object to any format that php date() accepts.

//returns the date time in mysql time format
return $date_time->format('H:i:s');


One Comment on “How to Convert Time Zones in PHP”

  1. Dorinda says:

    Superb know-how! I have been browsing for everything such as this for quite a while today. Thanks!


Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>