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>';
}
?>
timezone_identifiers_list
(PHP 5 >= 5.1.0)
timezone_identifiers_list — Returns numerically index array with all timezone identifiers
说明
array timezone_identifiers_list
( void
)
array DateTimeZone::listIdentifiers
( void
)
返回值
Returns array on success or FALSE on failure.
范例
Example#1 A timezone_identifiers_list() example
<?php
$timezone_identifiers = DateTimeZone::listIdentifiers();
for ($i=0; $i < 5; $i++) {
echo "$timezone_identifiers[$i]\n";
}
?>
上例的输出类似于:
Africa/Abidjan Africa/Accra Africa/Addis_Ababa Africa/Algiers Africa/Asmera
timezone_identifiers_list
vats_tco at comcast dot net
20-Nov-2007 02:14
20-Nov-2007 02:14
vats_tco at comcast dot net
17-Nov-2007 12:11
17-Nov-2007 12:11
Your script was useful, thanks. I made a few small changes to the script to make it work and display the Time Zones properly. The list would display the cities by country but some cities have sub-cities that are not displayed so you would see 8 Argentina's for example and the value would not get the proper Time Zone. With this fix it will display the sub-cites and add them to the value as well.
<?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'] = $zone[0];
$zonen[$i]['city'] = $zone[1];
$zonen[$i]['subcity'] = $zone[2];
$i++;
}
asort($zonen);
foreach($zonen AS $zone) {
extract($zone);
if($continent == 'Africa' OR $continent == 'America' OR $continent == 'Antarctica' OR $continent == 'Arctic' OR $continent == 'Asia' OR $continent == 'Atlantic' OR $continent == 'Australia' OR $continent == 'Europe' OR $continent == 'Indian' OR $continent == 'Pacific') {
if(!isset($selectcontinent)) {
$structure .= '<optgroup label="'.$continent.'">'; // continent
} elseif($selectcontinent != $continent) {
$structure .= '</optgroup><optgroup label="'.$continent.'">'; // continent
}
if($city != ''){
if ($subcity != ''){
$city = $city . '/'. $subcity;
}
$structure .= "<option ".((($continent.'/'.$city)==$selectedzone)?'selected="selected "':'')." value=\"".($continent.'/'.$city)."\">".str_replace('_',' ',$city)."</option>"; //Timezone
} else {
if ($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>';
}
?>
spam1 at dyden dot de
04-Jul-2007 09:22
04-Jul-2007 09:22
Hi!
Since many are going to use these timezones for saving their users' timezones, I wrote a little something that builds the select-field for this purpose and removes the "Others" that are not supposed to be used.
Note that <optgroup> is not widely supported and causes a bug in Opera 6.
I know this is easy to do, but you need to examine the timezones first (for example to remember that Istanbul is in the category for two continents - of course).
<select name="timezonechoice" size="10"><?php
function timezonechoice($selectedzone) {
$all = timezone_identifiers_list();
$i = 0;
foreach($all AS $zone) {
$zone = explode('/',$zone);
$zonen[$i]['continent'] = $zone[0];
$zonen[$i]['city'] = $zone[1];
$i++;
}
asort($zonen);
foreach($zonen AS $zone) {
extract($zone);
if($continent == 'Africa' OR $continent == 'America' OR $continent == 'Antarctica' OR $continent == 'Arctic' OR $continent == 'Asia' OR $continent == 'Atlantic' OR $continent == 'Australia' OR $continent == 'Europe' OR $continent == 'Indian' OR $continent == 'Pacific') {
if(!isset($letztercontinent)) $structure .= '
<optgroup label="'.$continent.'">'; // continent
elseif($letztercontinent!=$continent) $structure .= '</optgroup>
<optgroup label="'.$continent.'">'; // continent
if($city!='') $structure .= "
<option ".((($continent.'/'.$city)==$selectedzone)?'selected="selected "':'')." value=\"".($continent.'/'.$city)."\">".str_replace('_',' ',$city)."</option>"; //Timezone
else $structure .= "
<option ".(($continent==$selectedzone)?'selected="selected "':'')." value=\"".$continent."\">".$continent."</option>"; //Timezone
$letztercontinent = $continent;
}
}
$structure .= '
</optgroup>';
return $structure;
}
echo timezonechoice('Europe/Berlin');
?></select>
Hope this saves someone some effort.
