尝试多次设置时区时PHP发生冲突

时间:2009-12-31 19:19:44

标签: php timezone

我将用户时区设置在我的页面标题中,在我的设置区域中,但是用户可以选择时区,我试图在我的下拉时区列表中显示实际时间。

当前时间已经从我设置上面的页面时区更改,因此已经更改的时间再次更改

<?PHP
date_default_timezone_set($_SESSION['time_zone']);
?>

以上设置时区,下面是我的代码,它贯穿一系列时区并构建一个表单下拉列表,它将包含我所有的时区,并显示我当前保存到会话变量中的那个。它还会显示列表中每个时区名称旁边的当前时间。

问题是,我认为以前设置的页面时区,上面的代码,我认为它在我的下拉列表中弄乱了时间,如果我在页面上显示当前时间,那对我的时间是正确的区域,如果我在下拉列表中查看时间,它们是休息时间,任何想法我怎么能这样做?

<?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>';
?>

1 个答案:

答案 0 :(得分:0)

通过将date()更改为gmdate()获取当前时间,我能够正常工作

相关问题