Return to Snippet

Revision: 45930
at May 11, 2011 07:35 by kendsnyder


Updated Code
<?php 

function getTimezoneOptions() {
	$abbr = DateTimeZone::listAbbreviations();
	$options = array();
	foreach ($abbr as $section => $zones) {
		foreach ($zones as $zone) {
			if (!$zone['timezone_id']) {
				continue;
			}
			if (isset($options[$zone['timezone_id']])) {
				continue;
			}
			if (strpos($zone['timezone_id'], 'GMT') !== false) {
				// ignore the plain GMT zones
				continue;
			}
			$offset = round($zone['offset'], 2) / 3600;
			$hours = floor($offset);
			$minutes = ($offset - $hours) * 60;
			$minutes = $minutes == 0 ? '00' : $minutes;
			// ignore the weird ones
			if (!in_array($minutes, array('00','15','30','45'))) {
				continue;
			}
			$sign = substr($hours,0,1) == '-' ? '-' : '+';
			$hours = abs($hours);
			$options[$zone['timezone_id']] = str_replace('_', ' ', $zone['timezone_id']) . " ($sign$hours:$minutes)";
		}
	}
	asort($options);
	return $options;
}

// example output:
Array
(
    [Africa/Abidjan] => Africa/Abidjan (+0:00)
    [Africa/Accra] => Africa/Accra (+0:00)
    [Africa/Addis_Ababa] => Africa/Addis Ababa (+3:00)
    [Africa/Algiers] => Africa/Algiers (+2:00)
    [Africa/Asmara] => Africa/Asmara (+3:00)
	...
)

Revision: 45929
at May 11, 2011 07:31 by kendsnyder


Updated Code
<?php 

function getTimezoneOptions() {
	$abbr = DateTimeZone::listAbbreviations();
	$options = array();
	foreach ($abbr as $section => $zones) {
		foreach ($zones as $zone) {
			if (!$zone['timezone_id']) {
				continue;
			}
			if (isset($options[$zone['timezone_id']])) {
				continue;
			}
			if (strpos($zone['timezone_id'], 'GMT') !== false) {
				// ignore the plain GMT zones
				continue;
			}
			$offset = round($zone['offset'], 2) / 3600;
			$hours = floor($offset);
			$minutes = ($offset - $hours) * 60;
			$minutes = $minutes == 0 ? '00' : $minutes;
			// ignore the weird ones
			if (!in_array($minutes, array('00','15','30','45'))) {
				continue;
			}
			$sign = substr($hours,0,1) == '-' ? '-' : '+';
			$hours = abs($hours);
			$options[$zone['timezone_id']] = str_replace('_', ' ', $zone['timezone_id']) . " ($sign$hours:$minutes)";
		}
	}
	$options = array_unique($options);
	sort($options);
	return $options;
}

Revision: 45928
at May 11, 2011 07:30 by kendsnyder


Initial Code
<?php

function getTimezoneOptions() {
	$abbr = DateTimeZone::listAbbreviations();
	$options = array();
	foreach ($abbr as $section => $zones) {
		foreach ($zones as $zone) {
			if ($zone['timezone_id']) {
				if (isset($options[$zone['timezone_id']])) {
					continue;
				}
				if (strpos($zone['timezone_id'], 'GMT') !== false) {
					// ignore the plain GMT zones
					continue;
				}
				$offset = round($zone['offset'], 2) / 3600;
				$hours = floor($offset);
				$minutes = ($offset - $hours) * 60;
				$minutes = $minutes == 0 ? '00' : $minutes;
				// ignore the weird ones
				if (!in_array($minutes, array('00','15','30','45'))) {
					continue;
				}
				$sign = substr($hours,0,1) == '-' ? '-' : '+';
				$hours = abs($hours);
				$options[$zone['timezone_id']] = str_replace('_', ' ', $zone['timezone_id']) . " ($sign$hours:$minutes)";
			}
		}
	}
	$options = array_unique($options);
	sort($options);
	return $options;
}

Initial URL

                                

Initial Description

                                

Initial Title
PHP List of Known Time Zones

Initial Tags
date

Initial Language
PHP