php教程

timezone_identifiers_list

(PHP 5 >= 5.2.0, PHP 7)

timezone_identifiers_list别名 DateTimeZone::listIdentifiers()

说明

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

User Contributed Notes

jd dot aus dot st at gmail dot com 13-Jul-2018 02:48
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).
paul at burney dot ws 06-May-2009 01:33
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.
david at mytton dot net 22-Feb-2009 08:11
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 19-Nov-2007 10:14
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>';
}
?>

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788