PHP conflict when trying to set timezone more than once
I set the users timezone in the header of my page, in my setting area though where a user can pick a timezone, I am trying to show the actual time in my dropdown list of time zones.
the current time is already changed from me setting the pages timezone above, so the already changed time is getting changed again
<?PHP
date_default_timezone_set($_SESSION[开发者_运维知识库'time_zone']);
?>
Above set the time zone, below is my code that runs through an array of time zones and builds a form dropdown list, it will have all my time zones and show which one I currently have saved into the session variable. It also shows the current time next to the name to each time zone in my list.
The problem is, I think the previous set time zone for the page, the code above, I think it is messing the times in my dropdown list, If I show the current time on the page, it is correct to my time zone, if I look at times in the dropdown list, they are hours off, any idea how I can do this?
<?PHP
// the time zone array would be here
echo '<select name="time_zone_list" id="time_zone_list"/>';
$_SESSION['time_zone'] = (isset($_SESSION['time_zone'])) ? $_SESSION['time_zone'] : '';
// the current time is already changed from me setting the pages timezone above, so the already changed time is getting changed again
$current_date = date("h:i:s A");
foreach ($time_zone_array as $tz_id => $tz_name) {
$selected = "";
if ($tz_id == $_SESSION['time_zone']) $selected = ' selected="selected"';
//build time to show user in dropdown
$dt_obj = new DateTime($current_date." UTC");
$dt_obj->setTimezone(new DateTimeZone($tz_id));
print '<option value=' . $tz_id . $selected . '> ' .date_format($dt_obj, 'h:i:s A'). ' ' . $tz_name . '</option>';
}
echo '</select>';
?>
I was able to get it working correctly by changing date() to gmdate() for my current time
精彩评论