php:通过 UTC 偏移量设置时区
使用 javascript 我知道我的用户时区是 UTC +3。
现在我想用这些知识创建 DateTime 对象:
$usersNow = new DateTime('now', new DateTimeZone("+3"));
我收到响应:
'Unknown or bad timezone (+2)'
我做错了什么?我该如何修复?
Using javascript I know that my users timezone is UTC +3.
Now I want to create DateTime object with this knowledge:
$usersNow = new DateTime('now', new DateTimeZone("+3"));
I receive as a respsonse:
'Unknown or bad timezone (+2)'
What am I doing wrong? How can I fix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这个怎么样...
how about this...
你说:
您可能运行了类似这样的命令:
这将返回与 UTC 的当前偏移量(以分钟为单位),正值位于 UTC 以西。它不返回时区!
时区不是偏移量。时区有偏移量。它可以有多个不同的偏移量。通常有两种偏移,一种用于标准时间,另一种用于夏令时。单个数值无法单独代表这一点。
“America/New_York”
UTC-5
UTC-4
除了这两个偏移量之外,该时区中还包含以下日期和时间在两个偏移量之间进行转换,以便您知道它们何时应用。还有关于偏移和过渡如何随时间变化的历史记录。
另请参阅时区标签 wiki 中的“时区 != 偏移量”。
在您的示例中,您可能从 javascript 收到
-180
值,表示 UTC+3 的当前偏移量。但这只是该特定时间点的偏移量!如果您遵循 minaz 的回答,您将获得一个时区,该时区假设 UTC+3始终< /em> 正确的偏移量。如果实时时区类似于"Africa/Nairobi"
从未使用过除 UTC+3 之外的任何内容。但据您所知,您的用户可能位于
“Europe/Istanbul”
,夏季使用 UTC+3,冬季使用 UTC+2。You said:
You probably ran something like this:
This returns the current offset from UTC in minutes, with positive values falling west of UTC. It does not return a time zone!
A time zone is not an offset. A time zone has an offset. It can have multiple different offsets. Often there are two offsets, one for standard time and one for daylight saving time. A single numeric value cannot represent this alone.
"America/New_York"
UTC-5
UTC-4
Besides the two offsets, also wrapped up in that time zone are the dates and times for transitioning between the two offsets so you know when they apply. There's also a historical record of how the offsets and transitions may have changed over time.
See also "Time Zone != Offset" in the timezone tag wiki.
In your example case, you probably received a value of
-180
from javascript, representing a current offset of UTC+3. But that's just the offset for that particular point in time! If you follow minaz's answer, you will get a time zone that makes the assumption that UTC+3 is always the correct offset. That would work if the real time zone is something like"Africa/Nairobi"
which has never used anything except UTC+3. But for all you know your user could be in"Europe/Istanbul"
, which uses UTC+3 in the summer and UTC+2 in the winter.现代答案:
文档:
Modern answer:
Documentation:
从 PHP 5.5.10 开始,DateTimeZone 接受像“+3”这样的偏移量:
https://3v4l.org/NUGSv
Since PHP 5.5.10, DateTimeZone accepts an offset like "+3" :
https://3v4l.org/NUGSv
这使 Matthew 的答案更进一步,将日期的时区更改为任何整数偏移量。
注意:由于接受的答案,我在生产中遇到了中断。
This one takes Matthew's answer a step further to change the timezone of a date to any integer offset.
Note: I had a breakage in production due to the accepted answer.
您是否尝试使用
strtotime()
?Did you try using
strtotime()
?据我从 DateTimeZone 上的文档得知,您需要传递有效的时区,这里是有效的时区。检查其他,那里的东西可能会帮助你。
As far as I can tell from the docs on DateTimeZone, you need to pass a valid time zone and here are the valid ones. Check the others, something there may help you.
DateTimeZone 需要时区而不是offest
DateTimeZone requires a timezone not an offest
对于遇到此问题的任何人,我都面临着同样的问题,因此最终我扩展了 DateTime 类并覆盖了 __construct() 方法以接受偏移量(以分钟为单位)而不是时区。
从那里,我的自定义
__construct()
计算出以小时和分钟为单位的偏移量(例如 -660 = +11:00),然后使用parent::__construct()
将我的日期(自定义格式以包含我的偏移量)交回原始日期时间。因为我总是在应用程序中处理 UTC 时间,所以我的类还通过减去偏移量来修改 UTC 时间,因此传递午夜 UTC 和 -660 的偏移量将显示上午 11 点。
我的解决方案详细信息如下: https://stackoverflow.com/a/35916440/2301484
For anyone who comes across this, I was facing the same problem, so in the end I extended the DateTime class and overrode the
__construct()
method to accept an offset (in minutes) instead of a timezone.From there, my custom
__construct()
works out what the offset is in hours and minutes (e.g. -660 = +11:00) and then usesparent::__construct()
to hand my date, custom formatted to include my offset, back to the original DateTime.Because I'm always dealing with UTC times in my application, my class also modifies the UTC time by subtracting the offset, so passing Midnight UTC and an offset of -660 will show me 11am
My solution is detailed here: https://stackoverflow.com/a/35916440/2301484
感谢 Joey Rivera 的链接,我找到了一个解决方案。就像其他人在这里所说的那样,时区不是偏移量,您确实需要一个有效的时区。
这就是我自己使用的,
我自己发现使用 YYYY-MM-DD HH:MM 格式要方便得多。例子。
I was directed to a solution thanks to Joey Rivera's link. Like what the others have said here, timezone is not an offset you do need a valid timezone.
This is what I am using for myself
I myself have found it to be much more convenient to work with YYYY-MM-DD HH:MM format. example.