How to Convert Time Zones in PHP
Posted: May 9, 2011 | Author: eatmyswan | Filed under: Programming | 1 Comment » 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');
Superb know-how! I have been browsing for everything such as this for quite a while today. Thanks!