在PHP中设置时区偏移量

编程入门 行业动态 更新时间:2024-10-28 16:27:23
本文介绍了在PHP中设置时区偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在编写一个API,它将从传感器中获取读数,并返回一个时间和值的列表,使用Javascript的New Date()进行偏移。(见下文) p>

使用 $ date-> sub(DateInterval :: createFromDateString($ offset))可以获得加时/减法的时间。分钟)),但返回的时间和日期的偏移量为+00:00(例如 2016-02-26T13:32:28 + 00:00 而不是 2016-02-26T13:32:28 + 11:00 为澳大利亚)。

这会导致像Pebble或AngularJS这样的问题,它们在看到偏移量为+00:00之后,将自己的偏移量应用于我自己的偏移量调用 $ date->格式(c)时,如何正确设置偏移量?我应该自己编译日期(例如 $ date-> format(Ymd\TH:i:s。$ plusOrMinus。$ myoffset-> format(h:i)) / code>)还是有更好的方法?

编辑:由于平台的限制(例如Pebble智能手表),我不能,或不要,使用时区名称,因为在我的手表应用程序中实现时区菜单会打破UX或驱动文件大小,如果我添加一个偏移到时区表/库

解决方案

我把这个排序到底。来自 PHP手册的一些信息让我失望:

当$ time参数是UNIX时间戳(例如@ 946684800)或指定时区(例如2010)时,$ timezone参数和当前时区将被忽略-01-28T15:00:00 + 02:00)。

所以我最终的解决办法是扩展 DateTime 类,并覆盖 __ construct()方法。我修改过的类如下:

<?php class timezone extends DateTime { //覆盖我们的__construct方法函数__construct($ date =now,$ offset = null){ //如果我们没有指定偏移量 if(is_null($ offset)){ //假设UTC $ offsetFormat =+00:00; $ offset = 0; //否则.. } else { //创建一个新的DateTime,并获得另一个新的DateTime,并从中减去$ offset分钟。将结果格式化为+11:00或-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); } //接下来,我们从$ date获取偏移量。如果这个偏移(除以60,因为我们在几分钟内工作,而不是在几秒钟内)不等于我们的偏移 if((new DateTime($ date)) - > getOffset()/ 60!== $ offset){ //覆盖$ date,并将其设置为一个新的DateTime,并从中扣除$ offset分钟 $ date =(new DateTime($ date,new DateTimeZone(UTC)) ) - > sub(DateInterval :: createFromDateString($ offset。minutes)); //如果$ date的偏移量等于$ offset } else { //已经应用了一个偏移量(我们知道这是因为我们所有的预先偏移日期将以UTC为单位),我们不需要再次执行 $ date =(new DateTime($ date)); } //最后,将其返回原始的DateTime类。这种格式的作用如下所示:2016-03-10T23:16:37 + 11:00 parent :: __ construct($ date-> format(Ymd\TH:i:s)。 $ offsetFormat,null); } } echo(new timezone()) - > format(c)。 < br />; //会输出一些类似的内容,例如:03:17:44 + 00:00 echo(new timezone(null,-660)) - > format(c)。 < br />; //会输出一些类似的东西,如:03:17:44 + 11:00 echo(新时区(午夜,-660)) - >格式(c)。 < br />; //将输出2016-03-10T11:00:00 + 11:00 echo(新时区(午夜)) - >格式(c)。 < br />; //将输出第3〜10T00:00:00 + 00:00 echo(新时区(2016-01-0100:00 + 00:00,-660)) - >格式( C) 。 < br />; //将输出______ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ C) 。 < br />; //将会输出______01T11:00:00 + 11:00。请注意,偏移量不应用两次! ?>

编辑:这是一个我已经开源的图书馆。 查看GitHub

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(). (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 (e.g. 2016-02-26T13:32:28+00:00 instead of 2016-02-26T13:32:28+11:00 for Australia).

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

How can I correctly set an offset when calling $date->format("c")? Should I compile the date myself (e.g. $date->format("Y-m-d\TH:i:s" . $plusOrMinus . $myoffset->format("h:i"))) or is there a better way to do it?

EDIT: Due to platform limitations (e.g. 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

解决方案

I got this sorted in the end. This bit of info from the PHP manual tipped me off:

The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).

So my eventual solution was to extend the DateTime class and override the __construct() method. 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

更多推荐

在PHP中设置时区偏移量

本文发布于:2023-10-24 10:00:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523648.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时区   偏移量   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!