Created
October 15, 2016 20:14
-
-
Save davidhemphill/bf7551f7c209d760b54e6faedc363a0c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
if (!function_exists('timezone_list')) { | |
function timezone_list() | |
{ | |
static $regions = [ | |
DateTimeZone::AFRICA, | |
DateTimeZone::AMERICA, | |
DateTimeZone::ANTARCTICA, | |
DateTimeZone::ASIA, | |
DateTimeZone::ATLANTIC, | |
DateTimeZone::AUSTRALIA, | |
DateTimeZone::EUROPE, | |
DateTimeZone::INDIAN, | |
DateTimeZone::PACIFIC, | |
]; | |
return collect($regions)->flatMap(function ($region) { | |
return DateTimeZone::listIdentifiers($region); | |
})->map(function ($timezone) { | |
$zone = new DateTimeZone($timezone); | |
return [$timezone, $zone->getOffset(new DateTime)]; | |
}) | |
->toAssoc() | |
->ksort() | |
->map(function ($offset, $timezone) { | |
$prefix = $offset < 0 ? '-' : '+'; | |
$formatted = gmdate('H:i', abs($offset)); | |
return "(UTC{$prefix}{$formatted}) {$timezone}"; | |
}); | |
} | |
} |
This file contains hidden or 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 | |
if (!function_exists('timezone_list')) { | |
function timezone_list() | |
{ | |
static $regions = [ | |
DateTimeZone::AFRICA, | |
DateTimeZone::AMERICA, | |
DateTimeZone::ANTARCTICA, | |
DateTimeZone::ASIA, | |
DateTimeZone::ATLANTIC, | |
DateTimeZone::AUSTRALIA, | |
DateTimeZone::EUROPE, | |
DateTimeZone::INDIAN, | |
DateTimeZone::PACIFIC, | |
]; | |
$timezones = []; | |
foreach ($regions as $region) { | |
$timezones = array_merge($timezones, DateTimeZone::listIdentifiers($region)); | |
} | |
$offsets = []; | |
foreach ($timezones as $timezone) { | |
$zone = new DateTimeZone($timezone); | |
$offsets[$timezone] = $zone->getOffset(new DateTime); | |
} | |
ksort($offsets); | |
$timezone_list = []; | |
foreach ($offsets as $timezone => $offset) { | |
$offset_prefix = $offset < 0 ? '-' : '+'; | |
$offset_formatted = gmdate('H:i', abs($offset)); | |
$pretty_offset = "UTC${offset_prefix}${offset_formatted}"; | |
$timezone_list[$timezone] = "(${pretty_offset}) $timezone"; | |
} | |
return $timezone_list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment