Skip to content

Instantly share code, notes, and snippets.

@michaellehmkuhl
Created April 9, 2012 19:26
Show Gist options
  • Save michaellehmkuhl/2345870 to your computer and use it in GitHub Desktop.
Save michaellehmkuhl/2345870 to your computer and use it in GitHub Desktop.
PHP timezone conversion functions
/**
* 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