Skip to content

Instantly share code, notes, and snippets.

@jwhulette
Last active November 19, 2020 03:52
Show Gist options
  • Save jwhulette/62720a7f68c19edfdb26f951e2d6e689 to your computer and use it in GitHub Desktop.
Save jwhulette/62720a7f68c19edfdb26f951e2d6e689 to your computer and use it in GitHub Desktop.
[PHP Timezones] #php
/**
* Timezone calculations
*/
class Timezone
{
/**
* Timezones list with GMT offset
*
* @return array
* @link http://stackoverflow.com/a/9328760
* @link http://php.net/manual/en/datetimezone.listidentifiers.php
*/
public static function tzList()
{
$zones_array = array();
$timestamp = time();
foreach (timezone_identifiers_list(\DateTimeZone::PER_COUNTRY, 'US') as $key => $zone) {
date_default_timezone_set($zone);
$zones_array[$key]['zone'] = $zone;
$zones_array[$key]['zone_friendly'] = self::formatTimezoneName($zone);
$zones_array[$key]['diff_from_GMT'] = 'UTC/GMT ' . date('P', $timestamp);
}
return $zones_array;
}
/**
* Format the zone name into a friendly format
*
* @method formatTimezoneName
*
* @param string $name The zone nmae
*
* @return string The formatted zone name
*/
private static function formatTimezoneName($name)
{
$name = str_replace('/', ', ', $name);
$name = str_replace('_', ' ', $name);
$name = str_replace('St ', 'St. ', $name);
return $name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment