Created
July 6, 2013 02:25
-
-
Save christurnertv/5938379 to your computer and use it in GitHub Desktop.
PHP Class for handling time zone conversions.
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
<?php | |
class TimeShift { | |
public static $zones = array( | |
"America/New_York" => "Eastern", | |
"America/Chicago" => "Central", | |
"America/Denver" => "Mountain", | |
"America/Phoenix" => "Arizona", | |
"America/Los_Angeles" => "Pacific", | |
"America/Anchorage" => "Alaska", | |
"Pacific/Honolulu" => "Hawaii", | |
); | |
public static function utcNow() | |
{ | |
$date = new DateTime("now", new DateTimeZone('UTC')); | |
return $date->getTimestamp(); | |
} | |
// convert a local date string along with a specified timezone into a UTC timestamp for storage | |
public static function getUTCTimestamp($localDateString, $localTimeString, $localTimezone) | |
{ | |
$date = new DateTime($localDateString . ' ' . $localTimeString, new DateTimeZone($localTimezone)); | |
$date->setTimezone(new DateTimeZone('UTC')); | |
return $date->getTimestamp(); | |
} | |
// take a stored UTC timestamp along with a specified timezone and return a local datetime object | |
public static function getLocalDateTime($utcTimestamp, $localTimezone) | |
{ | |
$date = new DateTime("now", new DateTimeZone('UTC')); | |
$date->setTimestamp($utcTimestamp); | |
$date->setTimezone(new DateTimeZone($localTimezone)); | |
return $date; | |
} | |
public static function getPrintableZone($timezone) | |
{ | |
return self::$zones[$timezone]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment