【问题标题】:Subtracting users TimeZone offset (e.g. +02:00) from a timestamp从时间戳中减去用户时区偏移量(例如 +02:00)
【发布时间】:2016-09-30 17:49:33
【问题描述】:

背景:

我将时间列表显示为关联数组。数组如下所示(上午 11:00 到上午 12:00):

array(5) {
  [1475226000]=>
  string(35) "September 30, 2016, 11:00 am +02:00"
  [1475226900]=>
  string(35) "September 30, 2016, 11:15 am +02:00"
  [1475227800]=>
  string(35) "September 30, 2016, 11:30 am +02:00"
  [1475228700]=>
  string(35) "September 30, 2016, 11:45 am +02:00"
  [1475229600]=>
  string(35) "September 30, 2016, 12:00 pm +02:00"
}

键是一个unix时间戳。该值是在用户时区中显示的格式化 unix 时间戳。

我的代码

这是生成数组的注释类:

<?php

class Time
{
    public function __construct()
    {
        date_default_timezone_set('UTC');
    }

    public function getTimeSlots($year, $month, $day, $start_time = '11:00', $end_time = '12:00')
    {
        $date = $year . '-' .  $month . '-' . $day;
        // get GMT timestamp of 2016-09-30 00:00 Europe/London
        $gmt_date = strtotime($this->getRelativeDateTime($date));

        $gmt_date = $gmt_date - 7200;
        // subtract from or add to $gmt_date whatever our timezone offset in hours is

        // get start time offset in seconds from 2016-9-30 00:00
        $seconds_start = strtotime('1970-01-01 ' . $start_time . ' UTC');
        // get end time offset in seconds from 2016-9-30 00:00
        $seconds_end   = strtotime('1970-01-01 ' . $end_time . ' UTC');

        $unix_seconds_start = $gmt_date + $seconds_start; // GMT
        $unix_seconds_end   = $gmt_date + $seconds_end;

        // echo $unix_seconds_start . date('Y-m-d H:i', $unix_seconds_start);
        // echo '<br>';
        // echo $unix_seconds_end . date('Y-m-d H:i', $unix_seconds_end);

        while ($unix_seconds_start <= $unix_seconds_end) {

            $dt = new DateTime('@' . $unix_seconds_start);
            $dt->setTimezone(new DateTimeZone('Europe/Paris'));
            $slots[$unix_seconds_start] = $dt->format('F j, Y, H:i a P');
            $unix_seconds_start = $unix_seconds_start + 900;

        }
        echo '<pre>', var_dump($slots), '</pre>';
    }

    public function getRelativeDateTime($date)
    {
        $date = new DateTime($date, new DateTimeZone('Europe/Paris'));
        return $date->format('Y-m-d H:i');
    }
}

$time = new Time;
$time->getTimeSlots('2016', '09', '30');

// we want var_dump to show the following
// --------------------------------------
//
// array () {
//     from 00:00
//     1234567890 (unix timestamp) => '00:00' (users time)
//     1234567890 (unix timestamp) => '00:15' (users time)
//     1234567890 (unix timestamp) => '00:30' (users time)
//     1234567890 (unix timestamp) => '00:45' (users time)
//     to  24:00
// }

问题

对于用户,我希望我的时间严格从上午 00:00 开始,到下午 24:00 结束,但是如您所见,如果您运行代码,我会根据我的用户时间偏移量获得偏移量。

这意味着如果用户的时区为欧洲/伦敦 +01:00,我的数组从 01:00 开始。

问题出在第 16 行。如您所见,如果取消注释第 16 行并运行代码,它可以工作,但这只是因为我明确地从时间戳中减去了两个小时(以秒为单位)。

http://sandbox.onlinephpfunctions.com/code/d19b6fc5335f41af491dfedcfae2c390aa3000ec

问题

有没有办法使用 DateTime(或任何其他方法!)从 $gmt_date 变量中减去用户时区偏移量?

【问题讨论】:

    标签: php arrays date oop datetime


    【解决方案1】:

    这是你的答案。使用 DateTime 对象并创建一个时区。

    <?php
    
    $tz_array = array('UTC', 'Europe/Paris', 'Asia/Jerusalem');
    
    foreach ($tz_array as $tz) {
        $time = new Time($tz);
        $time->getTimeSlots('2016', '09', '30');
    }
    
    class Time {
    
        /** @var DateTimeZone */
        private $tz;
    
        public function __construct($tz) {
            $this->tz = new DateTimeZone($tz);
        }
    
        public function getTimeSlots($year, $month, $day, $start_time = '11:00', $end_time = '12:00') {
    
            $from = new DateTime("$year-$month-$day $start_time:00", $this->tz);
            $to = new DateTime("$year-$month-$day $end_time:00", $this->tz);
    
            $unix_seconds_start = $from->getTimestamp();
            $unix_seconds_end = $to->getTimestamp();
    
            $interval = new DateInterval('PT900S'); // 900 seconds interval
    
            $slots = array();
    
            while ($unix_seconds_start <= $unix_seconds_end) {
    
                $slots[$unix_seconds_start] = $from->format('F j, Y, H:i a P');
                $from->add($interval);
                $unix_seconds_start = $from->getTimestamp();
            }
    
            echo
            "Time slots demo for timezone ",
            $this->tz->getName();
            var_dump($slots);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-20
      • 2014-03-18
      • 1970-01-01
      • 1970-01-01
      • 2016-08-29
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 2011-04-02
      相关资源
      最近更新 更多