【问题标题】:PHP conflict when trying to set timezone more than once尝试多次设置时区时发生 PHP 冲突
【发布时间】:2009-12-31 19:19:44
【问题描述】:

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

当前时间已经从我上面设置的页面时区改变了,所以已经改变的时间又被改变了

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

【问题讨论】:

    标签: php timezone


    【解决方案1】:

    我能够通过将当前时间的 date() 更改为 gmdate() 来使其正常工作

    【讨论】:

      猜你喜欢
      • 2013-12-21
      • 2012-10-28
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 2018-01-21
      • 2021-05-30
      • 2017-07-03
      • 1970-01-01
      相关资源
      最近更新 更多