I typically use this to get a drop-down selection of timezones. Most of this originally came from the wordpress code base.
The function you’ll want to use to get the option elements is get_timezone_form_field_options($selectedValue)
/** * returns the list of timezones in an array
* @param bool $returnValues[optional] return an array of only the timezone values
* @return array timezones in format: $location[group/continent][php value]=display value; if $returnValues=true, timezone values returned as array
*/
function get_timezone_list($returnValues=false){
$locations=array(); //locations[continent/group][php value] = display value
$continents = array('Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific');
//add continent/city timezones
foreach (timezone_identifiers_list() as $zone){
$zone = explode('/', $zone); // 0 => Continent, 1 => City
//only use "friendly" continent names
if(in_array($zone[0], $continents)){
if(isset($zone[1]) && $zone[1]!=""){
if(isset($zone[2]) && $zone[1]!=""){
$locations[$zone[0]][$zone[0].'/'.$zone[1].'/'.$zone[2]] = str_replace('_', SPACE, $zone[1]).SPACE.'-'.SPACE.str_replace('_', SPACE, $zone[2]); // Creates array(DateTimeZone => 'Friendly name')
}
else{
$locations[$zone[0]][$zone[0].'/'.$zone[1]] = str_replace('_', SPACE, $zone[1]); // Creates array(DateTimeZone => 'Friendly name')
}
}
}
}
//add UTC timezone
$locations['UTC']['UTC']='UTC';
//add manual offset timezones
$offset_range = array (-12, -11.5, -11, -10.5, -10, -9.5, -9, -8.5, -8, -7.5, -7, -6.5, -6, -5.5, -5, -4.5, -4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 5.75, 6, 6.5, 7, 7.5, 8, 8.5, 8.75, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.75, 13, 13.75, 14);
foreach($offset_range as $offset){
$offset_name = ($offset>=0) ? '+'.$offset : (string)$offset;
$offset_value = $offset_name;
$offset_name = str_replace(array('.25','.5','.75'), array(':15',':30',':45'), $offset_name);
$offset_name = 'UTC'.$offset_name;
$offset_value = 'UTC'.$offset_value;
$locations['Manual Offset'][$offset_value]=$offset_name;
}
if($returnValues===true){
$temp=array();
foreach($locations as $key=>$val){
$temp[]=$key;
}
$locations=$temp;
}
return $locations;
}
function get_timezone_form_field_options($selectedvalue=""){
$output="";
$timezones = get_timezone_list();
$input_field=array();
foreach($timezones as $group => $group_val){
foreach($group_val as $value => $name){
$input_field[$group][] = array("name"=>$name, "value"=>$value);
}
}
foreach($input_field as $groupname => $group){
if($groupname===""){
$output.=getFormFieldOptions($group, $selectedvalue);
}
else{
$output.='<optgroup label="'.$groupname.'">';
$output.=getFormFieldOptions($group, $selectedvalue);
$output.='</optgroup>';
}
}
return $output;
}
//gets an individual option for an HTML select field and marks a match as selected when appropriate
function get_selectoption_value($name, $value, $selectedvalue=""){
if(!is_string($name)){ $name=$value; }
$selected="";
if($value==$selectedvalue){ $selected=' selected="selected"'; }
return '<option value="'.$value.'"'.$selected.'>'.$name.'</option>';
}
function getFormFieldOptions($group, $selectedvalue=""){
$optionTags="";
foreach($group as $option){
$value=$option['value'];
$name=(isset($option['name'])) ? $option['name'] : $value;
$optionTags.=get_selectoption_value($name, $value, $selectedvalue);
}
return $optionTags;
}