dongshi6710 2016-09-30 15:09
浏览 245
已采纳

使用DateTime和DateTimeZone在用户时区显示GMT不起作用

Background

I am trying to create an events scheduler that shows a days worth of events to a user. It looks like this.

enter image description here

On the left you can see the 15min time intervals, and underneath each interval is the corresponding unix timestamp.

problem:

Take two users for example.

Gemma is from london which is +01:00 GMT

Françoise comes from Paris which is +02:00 GMT

When Gemma looks at her events schedule each 15 minute row should show ...

  • a) the time in their timezone and
  • b) the corresponding unix timestamp

So take the timeslots from 10:00 AM to 11:00 AM GMT at 2016-9-30 for example, they would look something like this.

For Gemma:

  • 11:00 => 1475229600 (10:00 GMT)
  • 11:15 => 1475230500 (10:15 GMT)
  • 11:30 => 1475231400 (10:30 GMT)
  • 11:45 => 1475232300 (10:45 GMT)
  • 12:00 => 1475233200 (11:00 GMT)

For Françoise:

  • 12:00 => 1475229600 (10:00 GMT)
  • 12:15 => 1475230500 (10:15 GMT)
  • 12:30 => 1475231400 (10:30 GMT)
  • 12:45 => 1475232300 (10:45 GMT)
  • 13:00 => 1475233200 (11:00 GMT)

My Code

Here are my functions which generate the time slots

/**
 * intervals
 */
public function getTimeSlots($year, $month, $day, $start_time = '06:00', $end_time = '20:00')
{
    $date = $year . '-' . $month . '-' . $day; // 2016-9-30
    $unix_base_time = $this->getUnixTimeStamp($date); // unix timestamp for 2016-9-30 00:00

    $seconds_start = strtotime('1970-01-01 ' . $start_time . ' UTC'); // 21600 seconds
    $seconds_end   = strtotime('1970-01-01 ' . $end_time . ' UTC');   // 72000 seconds

    while ($seconds_start <= $seconds_end) {
        $slots[$unix_base_time + $seconds_start] = $this->getTime($date, '+' . $seconds_start . ' seconds');
        $seconds_start = $seconds_start + 900; // plus 15 minutes
    }
    return $slots;
}

/**
 * timestamps
 */
public function getUnixTimeStamp($offset = 'now')
{
    $date = new DateTime($offset, new DateTimeZone('UTC'));
    return $date->format('U'); // returns a unix timestamp
}


/**
 * time
 */
public function getTime($date = 'now', $offset = 'now')
{
    $date = new DateTime($date, new DateTimeZone($this->session->userdata('time_zone')));
    $date->modify($offset);
    return $date->format('H:i');
}

If I var_dump the output I get the following:

array(57) {
  [1475215200]=>
  string(5) "06:00"
  [1475216100]=>
  string(5) "06:15"
  [1475217000]=>
  string(5) "06:30"
  [1475217900]=>
  string(5) "06:45"
  [1475218800]=>
  string(5) "07:00"
  etc......
}

The unix timestamp is correct, but the times are displaying as GMT and not the users time zone!

So obviously I am doing something wrong in the getTime() function.

I can't work out why it is returning the time ('H:i') as GMT when I am clearly injecting the DateTimeZone object with the users timezone passed as an argument.

  • 写回答

1条回答 默认 最新

  • doupu1727 2016-09-30 15:18
    关注

    When creating a new DateTime object the timezone that you pass in is considered the timezone of the passed in date. In your case you are telling DateTime to interpret the seconds once as GMT+1 and once as GMT+2.

    Try the following instead:

    // our $date is in UTC because we derived it from unix timestamps
    $date = new DateTime($date,
           new DateTimeZone('UTC'));
    $date->modify($offset);
    // update the timezone. The time will be interpreted accordingly.
    $date->setTimeZone(new DateTimeZone($this->session>userdata('time_zone')));
    return $date->format('H:i');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出