Last active
August 29, 2015 14:10
-
-
Save wmeredith/b76e6160da323fbc5a78 to your computer and use it in GitHub Desktop.
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
<label for="timeZone">Select Timezone</label> | |
<select id="timeZone"> | |
<?php | |
function timezone_list() { | |
static $timezones = null; | |
if ($timezones === null) { | |
$timezones = []; | |
$offsets = []; | |
$now = new DateTime(); | |
foreach (DateTimeZone::listIdentifiers() as $timezone) { | |
$now->setTimezone(new DateTimeZone($timezone)); | |
$offsets[] = $offset = $now->getOffset(); | |
$timezones[$timezone] = '(' . format_GMT_offset($offset) . ') ' . format_timezone_name($timezone); | |
} | |
array_multisort($offsets, $timezones); | |
} | |
return $timezones; | |
} | |
function format_GMT_offset($offset) { | |
$hours = intval($offset / 3600); | |
$minutes = abs(intval($offset % 3600 / 60)); | |
return 'GMT' . ($offset ? sprintf('%+03d:%02d', $hours, $minutes) : ''); | |
} | |
function format_timezone_name($name) { | |
$name = str_replace('/', ', ', $name); | |
$name = str_replace('_', ' ', $name); | |
$name = str_replace('St ', 'St. ', $name); | |
return $name; | |
} | |
$cities = timezone_list(); | |
foreach($cities as $city) { | |
echo '<option value='.$key.'>'.$city.'</option>'; | |
}; | |
?> | |
</select> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment