Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created September 8, 2011 18:43
Show Gist options
  • Star 58 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save xeoncross/1204255 to your computer and use it in GitHub Desktop.
Save xeoncross/1204255 to your computer and use it in GitHub Desktop.
The perfect TimeZone selectbox list for PHP 5.3+
<?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>';
@mike131
Copy link

mike131 commented Feb 27, 2013

This is awesome!!!

@MartijnHols
Copy link

For anyone wondering, this generates a list like so:

<select id="timezone">
<optgroup label="Africa">
<option name="Africa/Abidjan">Abidjan - 22:18 (10:18 pm)</option>
................
<option name="Africa/Windhoek">Windhoek - 23:18 (11:18 pm)</option>
<optgroup>
<optgroup label="America">
<option name="America/Adak">Adak - 13:18 (1:18 pm)</option>
................
<option name="America/Argentina/San_Luis">Argentina/San_Luis - 19:18 (7:18 pm)</option>
................
</optgroup>
................
</select>

@uncgopher
Copy link

Should really change the

to

@kevdowney
Copy link

Nice script, just one typo "Aisa" should be "Asia" for anyone using this -- otherwise pefect!

@johnabela
Copy link

Without a doubt the best approach to displaying timezones in php that I have seen in my 15+ years of php development. Nice job.

@solankin1576
Copy link

Thanks @xeoncross!

@karim-khan
Copy link

Thank you very much it is a best script .

@Emirii
Copy link

Emirii commented Apr 24, 2017

Great, I never knew about this. Thank you for sharing!

@CristianGno
Copy link

Thank you for sharing!

@RoccoHoward
Copy link

Missing Australia as a timezone region.

@AtlasApollo
Copy link

A million thanks! This is awesome and I'll apply it! <3

@smamun56
Copy link

how to add GMT + in front before region ?

@peterjtracey
Copy link

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

@Abraham-Tourdex
Copy link

Abraham-Tourdex commented May 8, 2019

Where's Australia bro, what the hell?

'Australia' => DateTimeZone::AUSTRALIA,

Love the script but

@tkeil69575
Copy link

Great little snippet, thank you!
suggest you change

print '<option name="' . $timezone . '">' . $name . '</option>' . "\n";

to

print '<option value="' . $timezone . '">' . $name . '</option>' . "\n";

@TinySonhh
Copy link

Your 10 years ago script really save my day in 2021
Thank you very much for your contribution.

@rello86
Copy link

rello86 commented Jan 9, 2024

💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment