Created
April 9, 2012 19:26
-
-
Save michaellehmkuhl/2345870 to your computer and use it in GitHub Desktop.
PHP timezone conversion functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get the offset of the given timezone (in seconds) | |
*/ | |
function getTimeZoneOffset($timezone='') { | |
$tz_tmp = new DateTimeZone($timezone); | |
$dt_tmp = new DateTime('now', $tz_tmp); | |
$offset = $dt_tmp->getOffset(); | |
return $offset; | |
} | |
/** | |
* Convert the given time from the origin time zone to the destination time zone | |
*/ | |
function convertTime($datetime='0000-00-00 00:00:00', $origin_timezone='GMT', $destination_timezone='GMT', $output_as_mysql=false) { | |
$time = $datetime; | |
if (!is_int($time)) { | |
$time = strtotime($datetime); | |
} | |
$originOffset = $this->getTimeZoneOffset($origin_timezone); | |
$destinationOffset = $this->getTimeZoneOffset($destination_timezone); | |
$gmtTime = $time - $originOffset; | |
$convertedTime = $gmtTime + $destinationOffset; | |
if ($output_as_mysql) { | |
$convertedTime = date('Y-m-d H:i:s', $convertedTime); | |
} | |
return $convertedTime; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment