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 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试