php 如何获取本地服务器的时区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4700156/
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 14:01:58  来源:igfitidea点击:

How can I get the local server's time zone?

phpdatetimezone

提问by dvdx

How can I get the local server's timezone without relying on php.inior other configuration files?

如何在不依赖php.ini或其他配置文件的情况下获取本地服务器的时区?

I am looking for an output similar to the output of date('e'). eg. "UTC", "GMT", or "Atlantic/Azores".

我正在寻找类似于date('e'). 例如。“UTC”、“GMT”或“大西洋/亚速尔群岛”。

I need to know this so that I may know the MySQL timezone.

我需要知道这一点,以便我可以知道 MySQL 时区。

回答by John Parker

If you're using a Linux/Unix based hosting platform, you could use the output of the date command with the "alphabetic time zone abbreviation" formatter as such:

如果您使用的是基于 Linux/Unix 的托管平台,则可以将 date 命令的输出与“字母时区缩写”格式化程序一起使用:

$systemTimeZone = system('date +%Z');

However, it should be noted that you shouldn't necessarily rely on the system's timezone and that you should instead use date_default_timezone_set(or the date.timezonephp.ini setting) to set the required timezone.

但是,应该注意的是,您不一定要依赖系统的时区,而应该使用date_default_timezone_set(或date.timezonephp.ini 设置)来设置所需的时区。

回答by moinudin

If you're on *nix, call the system's dateusing popen:

如果您使用 *nix,请调用系统的dateusing popen

popen("date +%Z");

回答by Roman Gelembjuk

On linux/unix i use

在 linux/unix 上我使用

shell_exec("date +%Z");

回答by jgmjgm

These answers are not good. I don't know of a portable way to do it.

这些答案都不好。我不知道有什么便携式方法可以做到这一点。

For Linux here is one option: https://bojanz.wordpress.com/2014/03/11/detecting-the-system-timezone-php/

对于 Linux,这是一种选择:https: //bojanz.wordpress.com/2014/03/11/detecting-the-system-timezone-php/

The date solution others are listing might not be accurate as the problem is in Linux and the date utility (gnuutils, gnulib) will inherit the same problem.

其他人列出的日期解决方案可能不准确,因为问题出在 Linux 中,并且日期实用程序(gnuutils、gnulib)将继承相同的问题。

If I set the timezone to Europe/Berlin and then ask date for the timezone it will give me CET.

如果我将时区设置为欧洲/柏林,然后询问时区的日期,它会给我 CET。

The way to get the timezone in Linux is like this:

Linux中获取时区的方法是这样的:

 #include <time.h>
 #include <stdio.h>

 extern char *tzname[2];
 extern long timezone;
 extern int daylight;

 void main() {
         tzset();
         printf(tzname[0]);
 }

The problem is the zone files don't include their name, only the list of parent timezones or something like that.

问题是区域文件不包含它们的名称,只包含父时区列表或类似的东西。

CET is not the same as the Europe/Berlin zone. If I put CET in then dates can be calculated incorrectly. Sometimes this wont matter as some countries have been using one timezone for decades but occasionally it can matter.

CET 与欧洲/柏林区不同。如果我输入 CET,那么日期可能会计算错误。有时这并不重要,因为一些国家几十年来一直在使用一个时区,但有时它可能很重要。

Using zdump here is a diff between CET and Europe/Berlin:

在这里使用 zdump 是 CET 和欧洲/柏林之间的差异:

 >   Fri Mar 31 23:06:31 1893 UTC = Fri Mar 31 23:59:59 1893 LMT isdst=0 gmtoff=3208
 >   Fri Mar 31 23:06:32 1893 UTC = Sat Apr  1 00:06:32 1893 CET isdst=0 gmtoff=3600
 <   Sun Sep 16 00:59:59 1945 UTC = Sun Sep 16 02:59:59 1945 CEST isdst=1 gmtoff=7200
 <   Sun Sep 16 01:00:00 1945 UTC = Sun Sep 16 02:00:00 1945 CET isdst=0 gmtoff=3600
 <   Sun Apr  3 00:59:59 1977 UTC = Sun Apr  3 01:59:59 1977 CET isdst=0 gmtoff=3600
 ... SNIP about a dozen ...
 <   Sun Sep 30 01:00:00 1979 UTC = Sun Sep 30 02:00:00 1979 CET isdst=0 gmtoff=3600
 >   Wed May 23 23:59:59 1945 UTC = Thu May 24 01:59:59 1945 CEST isdst=1 gmtoff=7200
 ... SNIP couple dozen...
 >   Sun Oct  2 01:00:00 1949 UTC = Sun Oct  2 02:00:00 1949 CET isdst=0 gmtoff=3600

Some of these might not even matter if you're using a unixtime but you can clearly see here that in the 70s some would. There are likely a few other timezones with much more recent changes that would break is using the result from tzset. For many this would be a fringe issue but if it does effect you the result likely wont be pleasant.

如果您使用的是 unixtime,其中一些甚至可能无关紧要,但您可以在这里清楚地看到,在 70 年代有些会。可能还有一些其他时区的最近更改会使用 tzset 的结果进行破坏。对于许多人来说,这将是一个边缘问题,但如果它确实影响了您,结果可能不会令人愉快。

I do not know what the convention is for windows or if there is the same problem. There is an exception to this problem. If your system timezone is a root one (like GMT, UTC, CET, etc) then I don't think this problem should occur.

我不知道 Windows 的约定是什么,或者是否存在相同的问题。这个问题有一个例外。如果您的系统时区是根时区(如 GMT、UTC、CET 等),那么我认为不应出现此问题。

However your question is ultimately about MySQL:

但是,您的问题最终是关于 MySQL:

SELECT IF(@@global.time_zone = 'SYSTEM', @@global.system_time_zone, @@global.time_zone) AS time_zone;

If it is set to SYSTEM it will also suffer the same fate as PHP and tzset. I checked a friend's server and there's often an hour's difference because MySQL converts Europe/London to GMT. GMT does not include BST which is a cause of many problems. Internally MySQL might be fine because it's still using /etc/localtime which will point to the right file even if MySQL is reporting the wrong name but if you take that wrong name and use it to load the timezone with something else then the two may not have the same timezone.

如果它设置为 SYSTEM,它也将遭受与 PHP 和 tzset 相同的命运。我检查了一个朋友的服务器,通常会有一个小时的差异,因为 MySQL 将欧洲/伦敦转换为格林威治标准时间。GMT 不包括 BST,这是导致许多问题的原因。在内部 MySQL 可能没问题,因为它仍在使用 /etc/localtime,即使 MySQL 报告错误的名称,它也会指向正确的文件,但是如果您使用错误的名称并使用它来加载时区和其他内容,那么两者可能不会有相同的时区。

Even if you tried to include DST it would not be a perfect fix. If you MySQL instance is using SYSTEM you may instead want to see if you can convert the dates from that to UTC (or the PHP date/time) which might be painful for performance. In theory you could try to brute force detection but I wouldn't advise it. You could also try setting the timezone in the session variable and MySQL may convert automatically but take care to be sure and always RTM (if that doesn't work use the source).

即使您尝试包含 DST,它也不是一个完美的解决方案。如果您的 MySQL 实例正在使用 SYSTEM,您可能想查看是否可以将日期从该日期转换为 UTC(或 PHP 日期/时间),这可能会影响性能。从理论上讲,您可以尝试进行蛮力检测,但我不建议这样做。您还可以尝试在会话变量中设置时区,MySQL 可能会自动转换,但请注意确保始终使用 RTM(如果这不起作用,请使用源)。

回答by CD001

You don't actually need to know what "timezone"MySQL is running on - you merely need to know the difference to UTC.

您实际上并不需要知道MySQL 在哪个“时区”上运行——您只需要知道与UTC的区别。

To get that you can simply ask MySQL:

要获得它,您可以简单地询问 MySQL:

SELECT TIMESTAMPDIFF(HOUR, UTC_TIMESTAMP, NOW())

Since the value of NOW()is based on the timezone MySQL is running on, that will read the difference between NOW()and the UTC time, in hours. You can then use that value to create a full timestamp like 2016-04-15T15:52:01+01:00which can be used in DateTime::__construct().

由于 的值NOW()基于 MySQL 运行所在的时区,因此将读取NOW()UTC 时间与 UTC 时间之间的差异,以小时为单位。然后,您可以使用该值创建一个完整的时间戳,例如2016-04-15T15:52:01+01:00可以在DateTime::__construct().

You can then let PHP worry about the difference the timezones between the application and database servers by comparing DateTimeobjects... and that shouldwork on all systems.

然后,您可以让 PHP 通过比较DateTime对象来担心应用程序和数据库服务器之间的时区差异……这应该适用于所有系统。

回答by Nadeem Ijaz

you can get timezone of your server by this. echo date_default_timezone_get();

您可以通过此获取服务器的时区。echo date_default_timezone_get();