简体   繁体   English

在PHP中设置时区偏移

[英]Setting timezone offset in PHP

I'm writing an API that will retrieve readings from a sensor and return a list of times and values, offset using Javascript's `new Date(). 我正在编写一个API,该API将从传感器获取读数并返回时间和值的列表,并使用Javascript的new Date()进行补偿。 (see below for the reason) (原因请见下文)

I've been able to get the time addition / subtraction working well enough, using $date->sub(DateInterval::createFromDateString($offset . " minutes")) , but the time and date returned have an offset of +00:00 (eg 2016-02-26T13:32:28+00:00 instead of 2016-02-26T13:32:28+11:00 for Australia). 我已经可以使用$date->sub(DateInterval::createFromDateString($offset . " minutes"))来使时间加法/减法工作得足够好,但是返回的时间和日期的偏移量为+00: 00(例如,澳大利亚为2016-02-26T13:32:28+11:00 2016-02-26T13:32:28+00:00而不是2016-02-26T13:32:28+11:00 )。

This causes issues for things like the Pebble, or AngularJS, which apply their own offsets on top of my own, after seeing that the offset is +00:00 这会导致诸如Pebble或AngularJS之类的问题,它们在看到偏移量为+00:00之后,将偏移量应用到我自己的顶部

How can I correctly set an offset when calling $date->format("c") ? 调用$date->format("c")时如何正确设置偏移量? Should I compile the date myself (eg $date->format("Ymd\\TH:i:s" . $plusOrMinus . $myoffset->format("h:i")) ) or is there a better way to do it? 我应该自己编译日期(例如$date->format("Ymd\\TH:i:s" . $plusOrMinus . $myoffset->format("h:i")) )还是有更好的方法来编译日期?

EDIT: Due to platform limitations (eg the Pebble smartwatch) I can't, or don't want to, use timezone names, as implementing a timezone menu in my watch app would either break UX or drive the filesize up if I add an offset to timezone table / library 编辑:由于平台限制(例如,Pebble智能手表),我不能或不想使用时区名称,因为在我的手表应用程序中实现时区菜单会破坏UX或驱动文件大小,如果我添加了一个偏移到时区表/库

is this? 这是?

$date = new DateTime('now', new DateTimeZone('Australia/Adelaide'));
var_dump($date);
var_dump($date->format('c'));

the output is 输出是

object(DateTime)[2]
  public 'date' => string '2016-02-26 13:35:48.000000' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Australia/Adelaide' (length=18)
string '2016-02-26T13:35:48+10:30' (length=25)

I got this sorted in the end. 最后我得到了排序。 This bit of info from the PHP manual tipped me off: PHP手册中的以下信息提示我:

The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (eg @946684800) or specifies a timezone (eg 2010-01-28T15:00:00+02:00). 当$ time参数是UNIX时间戳(例如@ 946684800)或指定时区(例如2010-01-28T15:00:00 + 02:00)时,将忽略$ timezone参数和当前时区。

So my eventual solution was to extend the DateTime class and override the __construct() method. 因此,我最终的解决方案是扩展DateTime类并重写__construct()方法。 My modified class is as follows: 我修改过的类如下:

<?php

  class timezone extends DateTime {

    // Override our __construct method
    function __construct($date = "now", $offset = null) {
      // If we've not specified an offset
      if(is_null($offset)) {
          // Assume UTC
          $offsetFormat = "+00:00";
          $offset = 0;
      // Otherwise..
      } else {
        // Create a new DateTime, and get the difference between that, and another new DateTime that has $offset minutes subtracted from it. Format the results as something like +11:00 or -03:00
        $offsetFormat = (new DateTime($date, new DateTimeZone("UTC")))->diff((new DateTime($date, new DateTimeZone("UTC")))->sub(DateInterval::createFromDateString($offset . " minutes")))->format("%R%H:%I");
      }

      // Next, we get the offset from our $date. If this offset (divided by 60, as we're working in minutes, not in seconds) does NOT equal our offset
      if((new DateTime($date))->getOffset() / 60 !== $offset) {
        // Overwrite $date, and set it to a new DateTime with $offset minutes subtracted from it
        $date = (new DateTime($date, new DateTimeZone("UTC")))->sub(DateInterval::createFromDateString($offset . " minutes"));
      // If $date's offset equals $offset
      } else {
        // An offset has already been applied (we know this because all our pre-offset dates will be in UTC), and we don't need to do it again
        $date = (new DateTime($date));
      }

      // Finally, hand this back to the original DateTime class. This format works out to be something like: 2016-03-10T23:16:37+11:00
      parent::__construct($date->format("Y-m-d\TH:i:s") . $offsetFormat, null);
    }

  }


  echo (new timezone())->format("c") . "<br />"; // Will output something like 2016-03-10T12:17:44+00:00
  echo (new timezone(null, -660))->format("c") . "<br />"; // Will output something like 2016-03-10T23:17:44+11:00
  echo (new timezone("midnight", -660))->format("c") . "<br />"; // Will output 2016-03-10T11:00:00+11:00
  echo (new timezone("midnight"))->format("c") . "<br />"; // Will output 2016-03-10T00:00:00+00:00
  echo (new timezone("2016-01-01T00:00+00:00", -660))->format("c") . "<br />"; // Will output 2016-01-01T11:00:00+11:00
  echo (new timezone("2016-01-01T00:00+11:00", -660))->format("c") . "<br />"; // Will output 2016-01-01T11:00:00+11:00. Note that the offset isn't applied twice!

 ?>

EDIT: This is now a library that I've open-sourced. 编辑:这现在是我开源的库。 Check it out over on GitHub 在GitHub上查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM