I search the better solution for view the local time for all users of my website.
I got a problem with summer time, actually i got this function if you got a better way help me :
function timezone_by_offset($offset) {
$abbrarray = timezone_abbreviations_list();
$offset = $offset * 60 * 60;
foreach ($abbrarray as $abbr) {
foreach ($abbr as $city) {
if ($city['offset'] == $offset && $city['dst'] == TRUE) {
return $city['timezone_id'];
}
}
}
return 'UTC'; // any default value you wish
}
echo timezone_by_offset(1) . '<br/>';
echo (date_default_timezone_set(timezone_by_offset(1)) == TRUE ? 'Valid'. date('Y-m-d H:i:s') : 'Not Valid'). '<br/>';
SOLUTION :
<?php
function get_timezones()
{
$o = array();
$t_zones = timezone_identifiers_list();
foreach($t_zones as $a)
{
$t = '';
try
{
//this throws exception for 'US/Pacific-New'
$zone = new DateTimeZone($a);
$seconds = $zone->getOffset( new DateTime("now" , $zone) );
$hours = sprintf( "%+02d" , intval($seconds/3600));
$minutes = sprintf( "%02d" , ($seconds%3600)/60 );
$t = $a ." [ $hours:$minutes ]" ;
$o[$a] = $t;
}
//exceptions must be catched, else a blank page
catch(Exception $e)
{
//die("Exception : " . $e->getMessage() . '<br />');
//what to do in catch ? , nothing just relax
}
}
ksort($o);
return $o;
}
$o = get_timezones();
?>
<html>
<body>
<select name="time_zone">
<?php
foreach($o as $tz => $label)
{
echo "<option value="$tz">$label</option>";
}
?>
</select>
</body>
</html>
Best Regards.