给定 unix 时间戳,在 PHP 中获取时区偏移量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1536639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:59:54  来源:igfitidea点击:

Get timezone offset in PHP given a unix timestamp

phptimezonetimestampunix-timestamp

提问by Aistina

I have a Unix timestamp that comes from a database that is (unfortunately) in a timezone with a certain of either +3600 or +7200, depending on if it's DST or not. This has never been a problem before, but our website is currently moving to a more international audience and because of Javascript interaction with said timestamps, I need to convert the timestamp without any offset.

我有一个 Unix 时间戳,它来自一个数据库(不幸的是)在某个时区中,时区为 +3600 或 +7200,具体取决于它是否为 DST。这在以前从来都不是问题,但我们的网站目前正在面向更多国际受众,并且由于 Javascript 与上述时间戳的交互,我需要转换时间戳而没有任何偏移。

So, how do I, given just a Unix timestamp, get the offset for the timezone, at the moment of that timestamp? I'm looking through the PHP docs, but there's so many date/time functions I'm a bit lost.

那么,在仅给定 Unix 时间戳的情况下,我如何在该时间戳的时刻获取时区的偏移量?我正在浏览 PHP 文档,但有太多的日期/时间函数我有点迷茫。

I'm using PHP 5.2.3

我正在使用 PHP 5.2.3

回答by Aistina

I feel silly again answering my own question, but I just found the gmstrftimefunction. It seems to do exactly what I want. Thanks everyone though!

我再次回答我自己的问题感到很傻,但我刚刚找到了该gmstrftime功能。它似乎完全符合我的要求。不过还是谢谢大家!

回答by Dominic Rodger

You can't do that in the general case. A Unix timestamp is just the number of seconds since the Unix epoch, and does not contain any timestamp information.

在一般情况下你不能这样做。Unix 时间戳只是 Unix 纪元以来的秒数,不包含任何时间戳信息。

One thing that might work for you is working out whether or not a given timestamp occurs during a time when DST is in effect, and then adjust accordingly. That approach would fail around the boundaries though (since there are 2 meanings for each timestamp in the hour immediately after DST begins and immediately before it ends).

可能对您有用的一件事是确定给定的时间戳是否在 DST 生效期间出现,然后相应地进行调整。但是,这种方法会在边界附近失败(因为在 DST 开始之后和结束之前的一小时内,每个时间戳都有 2 个含义)。

In future, you're probably better off storing timestamps in UTC, to avoid precisely this sort of problem.

将来,您最好以 UTC 格式存储时间戳,以避免出现此类问题。

回答by Kjetil Joergensen

If you by unix timestamp mean unix time (seconds since Epoch), the offset is always 0. Epoch is defined as 00:00:00 UTC, January 1, 1970. Note the UTC, which has offset 0.

如果您使用 unix 时间戳表示 unix 时间(自纪元以来的秒数),则偏移量始终为 0。纪元定义为 00:00:00 UTC,1970 年 1 月 1 日。请注意偏移量为 0 的 UTC。

Application of timezone offset and dst is handled by the libraries that convert the unix timestamp into date/time structures. (I don't know php, but i.e. in C, the localtime function takes a unix timestamp, applies timezone information either from environment or the /etc/localtime file, and spits out a struct of seconds, minutes, hours, and so on).

时区偏移和 dst 的应用由将 unix 时间戳转换为日期/时间结构的库处理。(我不知道 php,但即在 C 中,localtime 函数采用 unix 时间戳,应用来自环境或 /etc/localtime 文件的时区信息,并吐出秒、分钟、小时等的结构)。

回答by Briganti

U can use this:

你可以使用这个:

$timezone_offset = date('O');

It will return the difference to Greenwich time (GMT) in hours.

它将以小时为单位返回格林威治时间 (GMT) 的时差。

[source]

[来源]

回答by Mike A.

Would this do the trick?

这能解决问题吗?

$tz = date("O", $time);

You could also do:

你也可以这样做:

From here:

从这里:

http://www.php.net/manual/en/function.date.php

http://www.php.net/manual/en/function.date.php