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 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码