Center windows with javascript | menu jquiry button | mysqli | use of cookies in php with example


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

 * Timezones list with GMT offset
 * @return array
 * @link
function tz_list() {
	$zones_array = array();
	$timestamp = time();
	foreach(timezone_identifiers_list() as $key => $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.


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'] ?>
		<?php } ?>


The result of the above code is:


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