Created
September 8, 2011 18:43
-
-
Save xeoncross/1204255 to your computer and use it in GitHub Desktop.
The perfect TimeZone selectbox list for PHP 5.3+
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 | |
$regions = 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 = array(); | |
foreach ($regions 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)); | |
// Us dumb Americans can't handle 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; | |
} | |
} | |
// View | |
print '<label>Select Your Timezone</label><select id="timezone">'; | |
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>'; |
Where's Australia bro, what the hell?
'Australia' => DateTimeZone::AUSTRALIA,
Love the script but
Great little snippet, thank you!
suggest you change
print '<option name="' . $timezone . '">' . $name . '</option>' . "\n";
to
print '<option value="' . $timezone . '">' . $name . '</option>' . "\n";
Your 10 years ago script really save my day in 2021
Thank you very much for your contribution.
💯
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have recently open-sourced a PHP class and corresponding jQuery class that consumes the JSON data the class generates. Somewhat the same concept as this snippet but much more sophisticated. It has a select list for region (defaults to US timezones) and an autocomplete select list for the timezones within the region. timezoneWidget
![timezonewidgetscreenshot](https://user-images.githubusercontent.com/4935218/40868197-9d968eec-65d8-11e8-9ede-417d6776bc38.png)