aHMAD_SQaLli Posted November 21, 2015 Share Posted November 21, 2015 Hello How can I create a user friendly timezone select menu using PHP, like the one in General account setting of PHPFreaks. thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/299546-how-to-create-timezone-select-menu-using-php/ Share on other sites More sharing options...
Barand Posted November 21, 2015 Share Posted November 21, 2015 Create a database table of the timezones and build it using that http://timezonedb.com/download 1 Quote Link to comment https://forums.phpfreaks.com/topic/299546-how-to-create-timezone-select-menu-using-php/#findComment-1526918 Share on other sites More sharing options...
Barand Posted November 21, 2015 Share Posted November 21, 2015 Forget that. You can use $tz_array = timezone_identifiers_list(); echo '<pre>',print_r($tz_array, true),'</pre>'; 1 Quote Link to comment https://forums.phpfreaks.com/topic/299546-how-to-create-timezone-select-menu-using-php/#findComment-1526920 Share on other sites More sharing options...
aHMAD_SQaLli Posted November 21, 2015 Author Share Posted November 21, 2015 Thanks, again one more thing, what can I add to the code to display the time offsets, like : (GMT +1:00 hours ) Brussels, Paris ... ? . Quote Link to comment https://forums.phpfreaks.com/topic/299546-how-to-create-timezone-select-menu-using-php/#findComment-1526921 Share on other sites More sharing options...
Solution Barand Posted November 21, 2015 Solution Share Posted November 21, 2015 perhaps $arr = timezone_identifiers_list(); foreach ($arr as $tz) { if (strpos($tz, '/')===false ) continue; list ($region, $city) = explode('/', $tz, 2); $d = new DateTimeZone($tz); $secs = $d->getOffset(new DateTime()); $offset = number_format($secs/3600, 2); $tzArray[$offset][$region][] = $city; } ksort($tzArray); $menu = "<option value=''>- select timezone -</option>\n"; foreach ($tzArray as $o => $regzones) { $menu .= sprintf("<optgroup label='GMT %+0.2f'>\n", $o); ksort($regzones); foreach ($regzones as $region => $cities) { foreach ($cities as $city) $menu .= "<option >$region/$city</option>\n"; } $menu .= "</optgroup>\n"; } ?> <select name='tzone'> <?=$menu?> </select> 1 Quote Link to comment https://forums.phpfreaks.com/topic/299546-how-to-create-timezone-select-menu-using-php/#findComment-1526924 Share on other sites More sharing options...
aHMAD_SQaLli Posted November 22, 2015 Author Share Posted November 22, 2015 Awesome, Thank you so much sir ! Quote Link to comment https://forums.phpfreaks.com/topic/299546-how-to-create-timezone-select-menu-using-php/#findComment-1526926 Share on other sites More sharing options...
Barand Posted November 22, 2015 Share Posted November 22, 2015 Alternative version if you prefer to group the timezones in the menu by region $arr = timezone_identifiers_list(); $now = new DateTime(); foreach ($arr as $tz) { if (strpos($tz, '/')===false ) continue; list ($region, $city) = explode('/', $tz, 2); $d = new DateTimeZone($tz); $secs = $d->getOffset($now); $offset = number_format($secs/3600, 2); $tzArray[$region][$offset][] = $city; } ksort($tzArray); $menu = "<option value=''>- select timezone -</option>\n"; foreach ($tzArray as $region => $regzones) { $menu .= sprintf("<optgroup label='%s'>\n", $region); ksort($regzones); foreach ($regzones as $o => $cities) { foreach ($cities as $city) $menu .= sprintf("<option value='$region/$city'>GMT%+0.2f $region/$city</option>\n", $o); } $menu .= "</optgroup>\n"; } ?> <select name='tzone'> <?=$menu?> </select> 2 Quote Link to comment https://forums.phpfreaks.com/topic/299546-how-to-create-timezone-select-menu-using-php/#findComment-1526936 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.