Open In App

How to generate a drop down list of timezone using PHP ?

Improve
Improve
Like Article
Like
Save
Share
Report

The timezone_identifiers_list() function is used to generate a dropdown list of timezone with PHP. This function is used to return an indexed array containing all the timezone identifiers. The datetimezone object is sent as a parameter to the timezone_identifiers_list() function and it returns an indexed array on success or False on failure. This function is an alias of DateTimeZone::listIdentifiers() function.
The timezone_identifiers_list() function uses its timezone constants and country to display a list of timezone independently.
Such that the possible values for timezone constants are:

1 = AFRICA | 2 = AMERICA | 4 = ANTARCTICA | 8 = ARCTIC | 16 = ASIA | 32 = ATLANTIC | 64 = AUSTRALIA | 128 = EUROPE | 256 = INDIAN | 512 = PACIFIC | 1024 = UTC | 2047 = ALL | 4095 = ALL_WITH_BC | 4096 = PER_COUNTRY

Syntax:

array timezone_identifiers_list( int $datetimezone, string $country )

Example 1: This example illustrates how to select the timezone listed in dropdown using timezone identifiers.




<?php
  
function select_Timezone($selected = '') {
  
    // Create a list of timezone
    $OptionsArray = timezone_identifiers_list();
        $select= '<select name="SelectContacts">
                    <option disabled selected>
                        Please Select Timezone
                    </option>';
          
        while (list ($key, $row) = each ($OptionsArray) ){
            $select .='<option value="'.$key.'"';
            $select .= ($key == $selected ? : '');
            $select .= '>'.$row.'</option>';
        
          
        $select.='</select>';
    return $select;
}
  
echo select_Timezone() . '<br>';
  
?>


Output:

Example 2: This example illustrates how to select the timezone listed in dropdown using timezone identifiers.




<?php
  
// Create a timezone identifiers
$timezone_identifiers
    DateTimeZone::listIdentifiers(DateTimeZone::ALL);
  
echo "<select>";
  
echo "<option disabled selected>
         Please Select Timezone
      </option>";
  
$n = 425;
for($i = 0; $i < $n; $i++) {
      
    // Print the timezone identifiers
    echo "<option value='" . $timezone_identifiers[$i] . 
        "'>" . $timezone_identifiers[$i] . "</option>";
}
  
echo "</select>";
  
?>


Output:

Example 3: This example illustrates the dropdown with list of TimeZone using DateTimeZone::listIdentifiers (DateTimeZone::ALL) along with range() function.




<?php
  
$timezone_identifiers
    DateTimeZone::listIdentifiers(DateTimeZone::ALL);
  
$Africa = range(0, 51);
$America = range(52, 198);
$Asia = range(211, 292);
$tz_stamp = time();
  
echo "<center><select style='padding:20px;
     font-family: Courier New, Courier, monospace;
     width: 450px;border:2px solid #a09;
     outline: none;'>";
  
echo "<option style='color:#FFF;font-family:Cambria;
          background-color:#a09;'><h3>Africa</h3>
      </option>";
  
foreach($Africa as $x) { 
    $tzone[$x] = date_default_timezone_set(
                        $timezone_identifiers[$x]);
      
    echo "<option>" . $timezone_identifiers[$x] .
            ' @ ' . date('P', $tz_stamp);"</option>";
}
  
echo "<option style='color:#FFF;font-family:Cambria;
        background-color:#a09;font-size:15px;'>
        <h3>America</h3></option>";
  
foreach($America as $x) { 
    $tzone[$x] = date_default_timezone_set(
                        $timezone_identifiers[$x]);
      
    echo "<option>" . $timezone_identifiers[$x] . 
            ' @ ' . date('P', $tz_stamp);"</option>";
}
  
echo "<option style='color:#FFF;font-family:Cambria;
        background-color:#a09;font-size:15px;'>
        <h3>Asia</h3></option>";
          
foreach($Asia as $x) { 
    $tzone[$x] = date_default_timezone_set(
                        $timezone_identifiers[$x]);
  
    echo "<option>" . $timezone_identifiers[$x] .
            ' @ ' . date('P', $tz_stamp);"</option>";
}
  
echo "</select></center>";
?>


Output:

Reference: https://www.php.net/manual/en/function.timezone-identifiers-list.php



Last Updated : 11 Oct, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads