Skip to content

Instantly share code, notes, and snippets.

@wmeredith
Last active August 29, 2015 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wmeredith/b76e6160da323fbc5a78 to your computer and use it in GitHub Desktop.
Save wmeredith/b76e6160da323fbc5a78 to your computer and use it in GitHub Desktop.
<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