Skip to content

Instantly share code, notes, and snippets.

@christurnertv
Created July 6, 2013 02:25
Show Gist options
  • Save christurnertv/5938379 to your computer and use it in GitHub Desktop.
Save christurnertv/5938379 to your computer and use it in GitHub Desktop.
PHP Class for handling time zone conversions.
<?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