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

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • del.icio.us
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • FriendFeed
  • Meneame
  • Netvibes
  • Reddit
  • StumbleUpon
  • Tumblr
  • Wikio
  • RSS
  • email
  • PDF
  • Print

Related posts:

  1. Input select generated from query – PHP
  2. Word to PDF in PHP
  3. passGenerator – Random passwords for PHP
  4. Count Words Repetitions in PHP
  5. webLab – Create your own php-html lab

Leave a Reply

Follow me