how to remove unused css rules in google chrome | msqli example | bind param array
 


 

Easily Create a List of Time Zones with PHP



Sometimes, you want to create a list of available time zones to prompt your user to select his/her time zone. Fortunately, PHP offers timezone_identifiers_list (PHP 5 >= 5.2.0). You can create your list easily, using a function like this

<?php
/**
 * Timezones list with GMT offset
 *
 * @return array
 * @link http://stackoverflow.com/a/9328760
 */
function tz_list() {
	$zones_array = array();
	$timestamp = time();
	foreach(timezone_identifiers_list() as $key => $zone) {
		date_default_timezone_set($zone);
		$zones_array[$key]['zone'] = $zone;
		$zones_array[$key]['diff_from_GMT'] = 'UTC/GMT ' . date('P', $timestamp);
	}
	return $zones_array;
}
?>

The above function returns an array with all timezones and the GMT offset for each timezone.

Example

A simple example:

<div style="margin-top: 20px;">
	<select style="font-family: 'Courier New', Courier, monospace; width: 450px;">
		<option value="0">Please, select timezone</option>
		<?php foreach(tz_list() as $t) { ?>
			<option value="<?php print $t['zone'] ?>">
				<?php print $t['diff_from_GMT'] . ' - ' . $t['zone'] ?>
			</option>
		<?php } ?>
	</select>
</div>

DEMO

The result of the above code is:

References

From PHP manual

Εγγραφή!
Sign-up for our free email newsletter. Get updates when new tutorials and tips are published. You can unsubscribe anytime with a click.

Your comments are welcomed!

This site actively encourages commenting on any post. Comments are not pre-moderated, but this community does not tolerate direct or indirect attacks, name-calling or insults. Please, read terms of use and Comment Policy at privacy policy.

More tips