Generating a List of Timezones with PHP

I often find myself developing applications that need to support users in different timezones, it is important to these users that dates are displayed in their preferred timezone. I thought I would share with you the function I use to create a list of timezones, each with their current UTC offset.

The following function will return a list of timezones ordered by their UTC offset and timezone identifier in ascending order.

Here is the format of the array which this function returns:

You could for example use this to generate an HTML drop down select of timezones using the following code:

I hope this comes in handy!

  • Great function! I added a bit to the function to also add what the current time would be in those timezones.

    In place of lines 32 and 33, I put this:

    // get current time in this timezone
    $time = new DateTime(NULL, new DateTimeZone($tz['identifier']));

    // convert to am and pm
    $ampm = ' ('. $time->format('g:ia'). ')';

    //create the display string
    $timezoneList[$tz['identifier']] = ' (UTC ' . $sign . $offset . ') ' .$tz['identifier'].' '. $time->format('H:i') . $ampm;