timezone_identifiers_list

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

timezone_identifiers_list别名 DateTimeZone::listIdentifiers()

说明

此函数是该函数的别名: DateTimeZone::listIdentifiers()

add a note add a note

User Contributed Notes 4 notes

up
4
jd dot aus dot st at gmail dot com
6 years ago
to create a list of all timezones using the code as the value, use the below:
<?php
function select_Timezone($selected = '') {
   
$OptionsArray = timezone_identifiers_list();
       
$select= '<select name="SelectContacts">';
        while (list (
$key, $row) = each ($OptionsArray) ){
           
$select .='<option value="'.$key.'"';
           
$select .= ($key == $selected ? ' selected' : '');
           
$select .= '>'.$row.'</option>';
        } 
// endwhile;
       
$select.='</select>';
return
$select;
}
?>

The above func takes one optional parameter which is the selected timezone, example for sydney Australia would be:
<?php
echo select_Timezone(313) . '<br>';
?>

If you wanted to just display the list (no pre-selected option):
<?php
echo select_Timezone() . '<br>';
?>

much easier to understand than the other options on this page (although not as flexible).
up
-7
david at mytton dot net
15 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
            ...
up
-10
vats_tco at comcast dot net
17 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>';
}
?>
up
-9
paul at burney dot ws
15 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