TimeZone select for PHP

Simple script that returns to us a list with possible TimeZones:

<select id="TIMEZONE" name="TIMEZONE">
<?php
$timezone_identifiers = DateTimeZone::listIdentifiers();
foreach( $timezone_identifiers as $value ){
     if ( preg_match( '/^(America|Antartica|Arctic|Asia|Atlantic|Europe|Indian|Pacific)\//', $value ) ){
	   echo "<option>$value</option>";
     }
}
?>
</select>

Improved version:

	<select id="TIMEZONE" name="TIMEZONE">
	<?php
	$timezone_identifiers = DateTimeZone::listIdentifiers();
	foreach( $timezone_identifiers as $value ){
	    if ( preg_match( '/^(America|Antartica|Arctic|Asia|Atlantic|Europe|Indian|Pacific)\//', $value ) ){
	    	$ex=explode("/",$value);//obtain continent,city	
	    	if ($continent!=$ex[0]){
	    		if ($continent!="") echo '</optgroup>';
	    		echo '<optgroup label="'.$ex[0].'">';
	    	}
 
	    	$city=$ex[1];
	    	$continent=$ex[0];
	    	echo '<option value="'.$value.'">'.$city.'</option>';	    		
	    }
	}
	?>
		</optgroup>
	</select>

Be aware this will only work in PHP 5.2.0 or greater as they explain in the doc

Related Posts Related Websites
Help sharing and Flatter me ;)

Leave a Reply

Follow me