Skip to content

Instantly share code, notes, and snippets.

@davidhemphill
Created October 15, 2016 20:14
Show Gist options
  • Save davidhemphill/bf7551f7c209d760b54e6faedc363a0c to your computer and use it in GitHub Desktop.
Save davidhemphill/bf7551f7c209d760b54e6faedc363a0c to your computer and use it in GitHub Desktop.
<?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}";
});
}
}
<?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