PHP 7.0.6 Released

timezone_identifiers_list

(PHP 5 >= 5.2.0, PHP 7)

timezone_identifiers_listAlias of DateTimeZone::listIdentifiers()

Description

This function is an alias of: DateTimeZone::listIdentifiers()

User Contributed Notes

david at mytton dot net
7 years ago
A better code snippet to return useful timezone information that can be used in a drop menu, for example:

<?php
$zones
= timezone_identifiers_list();
       
foreach (
$zones as $zone)
{
   
$zone = explode('/', $zone); // 0 => Continent, 1 => City
   
    // Only use "friendly" continent names
   
if ($zone[0] == 'Africa' || $zone[0] == 'America' || $zone[0] == 'Antarctica' || $zone[0] == 'Arctic' || $zone[0] == 'Asia' || $zone[0] == 'Atlantic' || $zone[0] == 'Australia' || $zone[0] == 'Europe' || $zone[0] == 'Indian' || $zone[0] == 'Pacific')
    {       
        if (isset(
$zone[1]) != '')
        {
           
$locations[$zone[0]][$zone[0]. '/' . $zone[1]] = str_replace('_', ' ', $zone[1]); // Creates array(DateTimeZone => 'Friendly name')
       
}
    }
}
?>

The $locations array will contain a multi-dimensional array for each continent like

Array
(
    [Africa] => Array
        (
            [Africa/Abidjan] => Abidjan
            [Africa/Accra] => Accra
            [Africa/Addis_Ababa] => Addis Ababa
            [Africa/Algiers] => Algiers
            ...
        )
    [America] => Array
        (
            [America/Adak] => Adak
            [America/Anchorage] => Anchorage
            [America/Anguilla] => Anguilla
            ...
vats_tco at comcast dot net
8 years ago
This is the updated code from below. This one has been debugged so it doesn't receive any warnings or errors like the code below will receive. Hope this helps.

<?php
function get_tz_options($selectedzone, $label, $desc = '')
{
  echo
'<div class="label"><label for="edited_user_timezone">'.$label.':</label></div>';
  echo
'<div class="input"><select name="edited_user_timezone">';
  function
timezonechoice($selectedzone) {
   
$all = timezone_identifiers_list();

   
$i = 0;
    foreach(
$all AS $zone) {
     
$zone = explode('/',$zone);
     
$zonen[$i]['continent'] = isset($zone[0]) ? $zone[0] : '';
     
$zonen[$i]['city'] = isset($zone[1]) ? $zone[1] : '';
     
$zonen[$i]['subcity'] = isset($zone[2]) ? $zone[2] : '';
     
$i++;
    }

   
asort($zonen);
   
$structure = '';
    foreach(
$zonen AS $zone) {
     
extract($zone);
      if(
$continent == 'Africa' || $continent == 'America' || $continent == 'Antarctica' || $continent == 'Arctic' || $continent == 'Asia' || $continent == 'Atlantic' || $continent == 'Australia' || $continent == 'Europe' || $continent == 'Indian' || $continent == 'Pacific') {
        if(!isset(
$selectcontinent)) {
         
$structure .= '<optgroup label="'.$continent.'">'; // continent
       
} elseif($selectcontinent != $continent) {
         
$structure .= '</optgroup><optgroup label="'.$continent.'">'; // continent
       
}

        if(isset(
$city) != ''){
          if (!empty(
$subcity) != ''){
           
$city = $city . '/'. $subcity;
          }
         
$structure .= "<option ".((($continent.'/'.$city)==$selectedzone)?'selected="selected "':'')." value=\"".($continent.'/'.$city)."\">".str_replace('_',' ',$city)."</option>"; //Timezone
       
} else {
          if (!empty(
$subcity) != ''){
           
$city = $city . '/'. $subcity;
          }
         
$structure .= "<option ".(($continent==$selectedzone)?'selected="selected "':'')." value=\"".$continent."\">".$continent."</option>"; //Timezone
       
}

       
$selectcontinent = $continent;
      }
    }
   
$structure .= '</optgroup>';
    return
$structure;
  }
  echo
timezonechoice($selectedzone);
  echo
'</select>';
  echo
'<span class="notes"> '.$desc.' </span></div>';
}
?>
paul at burney dot ws
6 years ago
Please note that the timezone_identifiers_list function is not available in the most recent versions of PHP available for CentOS/RHEL as of this writing ( 5.1.6-23.2.el5_3 ).

I think the function is labeled as >= 5.1.0 in this documentation because it's possible to get the Date::Time class installed in 5.1 if you're compiling from scratch (or it's a documentation error).

I just spent a bunch of time trying to get it to work in 5.1 (installing timezonedb manually because the default PECL runs out of memory, etc.), but have decided to use a manually generated list instead. The actual timezone support seems to work fine in 5.1.
To Top