Drop down of all timezones, grouped by Region, + a list of manual UTC offsets
Created
April 2, 2020 09:15
-
-
Save ozh/9981e14eff488e6e071913b817a18fbc to your computer and use it in GitHub Desktop.
PHP timezone dropdown
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
<?php | |
// Continent list | |
$continent = array( | |
'Africa' => DateTimeZone::AFRICA, | |
'America' => DateTimeZone::AMERICA, | |
'Antarctica' => DateTimeZone::ANTARCTICA, | |
'Aisa' => DateTimeZone::ASIA, | |
'Atlantic' => DateTimeZone::ATLANTIC, | |
'Europe' => DateTimeZone::EUROPE, | |
'Indian' => DateTimeZone::INDIAN, | |
'Pacific' => DateTimeZone::PACIFIC, | |
); | |
// Timezones per continents | |
$timezones = array(); | |
foreach ($continent as $name => $mask) { | |
$zones = DateTimeZone::listIdentifiers($mask); | |
foreach($zones as $timezone) { | |
// Lets sample the time there right now | |
$time = new DateTime(NULL, new DateTimeZone($timezone)); | |
// For Americans and their so called "millitary time" | |
$ampm = $time->format('H') > 12 ? ' ('. $time->format('g:i a'). ')' : ''; | |
// Remove region name and add a sample time | |
$timezones[$name][$timezone] = substr($timezone, strlen($name) + 1) . ' - ' . $time->format('H:i') . $ampm; | |
} | |
} | |
// Manual UTC offset | |
$offset_range = array( | |
-12, -11.5, -11, -10.5, -10, -9.5, -9, | |
-8.5, -8, -7.5, -7, -6.5, -6, -5.5, | |
-5, -4.5, -4, -3.5, -3, -2.5, -2, | |
-1.5, -1, -0.5, 0, 0.5, 1, 1.5, | |
2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, | |
5.75, 6, 6.5, 7, 7.5, 8, 8.5, | |
8.75, 9, 9.5, 10, 10.5, 11, 11.5, | |
12, 12.75, 13, 13.75, 14, | |
); | |
foreach( $offset_range as $offset ) { | |
if ( 0 <= $offset ) { | |
$offset_name = '+' . $offset; | |
} else { | |
$offset_name = (string) $offset; | |
} | |
$offset_value = $offset_name; | |
$offset_name = str_replace( array( '.25', '.5', '.75' ), array( ':15', ':30', ':45' ), $offset_name ); | |
$offset_name = 'UTC' . $offset_name; | |
$offset_value = 'UTC' . $offset_value; | |
$timezones['UTC'][$offset_value] = $offset_name; | |
} | |
// View | |
print '<label>Select Your Timezone</label><select id="timezone">'; | |
print '<option name="">(select)</option>'; | |
foreach($timezones as $region => $list) | |
{ | |
print '<optgroup label="' . $region . '">' . "\n"; | |
foreach($list as $timezone => $name) | |
{ | |
print '<option name="' . $timezone . '">' . $name . '</option>' . "\n"; | |
} | |
print '<optgroup>' . "\n"; | |
} | |
print '</select>'; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment