dongshi6710 2016-09-30 07:09
浏览 246
已采纳

使用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 07: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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部