Best way to populate a <SELECT> box with TimeZones

Viewed 25575

I need to display a timezone selector as a user control, which always seems easier than it really is. Internally, I store everything with a DateTimeZone Identifier, as that seems to be the smartest way to have the level of accuracy I need, as this project bridges real-world times as it is tied to terrestrial media.

What I don't want to do is present a select box with 300+ time zones, nor do I want to create faked timezone offsets with something like 'UTC-8' (which loses not only DST info, but the actual dates that the DST falls on).

In the end, I'll need a select with options containing the proper TZD Identifiers, something like this (the bracketed #s aren't necessary, just for potential end-user illustration):

<select>
<option value="America/Los_Angeles">Los Angeles [UTC-7 | DST]</option>
...
</select>

Does anyone have any pointers for building this list? All of the solutions I've googled have been problematic in one way or another.


I've added a bounty in case that might entice somebody to share a nicer answer with us. : )

12 Answers

Similar to the accepted answer, but alot shorter, and also shows the current time in each timezone:

foreach(timezone_identifiers_list() as $tz)
{
    $current_dt = new DateTime('now', new DateTimeZone($tz));

    echo '<option value=' . $tz . '>'
            . str_replace('_', ' ', $tz) . ' - ' // Some timezones contain _
            . $current_dt->format('\G\M\TP (H:i)') // Add the time in $tz
        . '</option>';
}

Outputs something like: Europe/Brussels - GMT+02:00 (23:57)
Any valid format (GMT == UTC) can be used for the timezone/current time.

Related