Quantcast
Channel: PHP Timezone List - Stack Overflow
Viewing all articles
Browse latest Browse all 15

Answer by chpx for PHP Timezone List

$
0
0

The below outputs the HTML <option>'s with all timezones specified in the $desiredRegions array.

function getAllTimeZones(){    $zoneIdentifiers = timezone_identifiers_list();    $zoneLocations = array();    foreach ($zoneIdentifiers as $zoneIdentifier)    {        $zone = explode('/', $zoneIdentifier);        $desiredRegions = array('Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific'        );        if (in_array($zone[0], $desiredRegions))        {            if (isset($zone[1]) != '')            {                $area = str_replace('_', '', $zone[1]);                if (!empty($zone[2]))                {                    $area = $area . ' (' . str_replace('_', '', $zone[2]) . ')';                }                $zoneLocations[$zone[0]][$zoneIdentifier] = $zone[0] . '/' .  $area;            }        }    }    $selectOptions = "";    foreach($zoneLocations as $zoneRegion => $regionAreas)    {        foreach($regionAreas as $regionArea => $zoneLabel)        {            $currentTimeInZone = new DateTime("now", new DateTimeZone($regionArea));            $currentTimeDiff = $currentTimeInZone->format('P');            $selectOptions .= "<option value=\"$regionArea\">(GMT $currentTimeDiff) $zoneLabel</option>\n";        }    }    return $selectOptions;}echo getAllTimeZones();

Output would look like:

<optgroup label="Africa"><option value="Africa/Abidjan">(GMT +00:00) Africa/Abidjan</option><option value="Africa/Accra">(GMT +00:00) Africa/Accra</option><option value="Africa/Addis_Ababa">(GMT +03:00) Africa/Addis Ababa</option>[...]</optgroup>

Based on this gist.


Viewing all articles
Browse latest Browse all 15

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>