timezone_identifiers_list
(PHP 5 >= 5.2.0)
timezone_identifiers_list — Псевдоним DateTimeZone::listIdentifiers()
Описание
Эта функция является псевдонимом: DateTimeZone::listIdentifiers()
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с датой и временем
- Дата и Время
- checkdate
- date_add
- date_create_from_format
- date_create_immutable_from_format
- date_create_immutable
- date_create
- date_date_set
- date_default_timezone_get
- date_default_timezone_set
- date_diff
- date_format
- date_get_last_errors
- date_interval_create_from_date_string
- date_interval_format
- date_isodate_set
- date_modify
- date_offset_get
- date_parse_from_format
- date_parse
- date_sub
- date_sun_info
- date_sunrise
- date_sunset
- date_time_set
- date_timestamp_get
- date_timestamp_set
- date_timezone_get
- date_timezone_set
- date
- getdate
- gettimeofday
- gmdate
- gmmktime
- gmstrftime
- idate
- localtime
- microtime
- mktime
- strftime
- strptime
- strtotime
- time
- timezone_abbreviations_list
- timezone_identifiers_list
- timezone_location_get
- timezone_name_from_abbr
- timezone_name_get
- timezone_offset_get
- timezone_open
- timezone_transitions_get
- timezone_version_get
Коментарии
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>';
}
?>
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
...
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 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).