所以我有一个用 PHP 编写的 Web 应用程序,它将在世界各地的不同 Ubuntu 服务器上运行。
有些服务器将配置为在本地时间运行,有些将在 UTC 运行,这取决于客户。
虽然我可以编辑 php.ini 文件并设置 date.timezone,但手动输入此类数据肯定有一天会出错。
如何让 PHP 使用系统中已定义的时区(tzdata)?
或者,换句话说:如何从 PHP 系统中提取(长)时区名称以在 date_default_timezone_set() 中使用?
So I have a web app written in PHP that will run on different Ubuntu servers around the world.
Some of the servers will be configured to run on local time, some will run on UTC, it depends on the customer.
While I can edit the php.ini-file and set date.timezone, manually enter such data is bound to get wrong one day.
How can I get PHP to use the time zone already defined in the system (tzdata)?
Or, in other words: How can I extract (the long) time zone name from the system in PHP to use in date_default_timezone_set()?
发布评论
评论(3)
在安德烈·克努普(Andrey Knupp)的回答中提供的帮助下,我能够解决这个问题。
给出输出:
更新:时区的简称不是唯一的。例如,在美国和澳大利亚都有一个“EST”,导致 timezone_name_from_abbr 有时会选择我不想要的那个...您可以询问 date 偏移量有多大,这可以用于进一步匹配正确的时区:
With the help that Andrey Knupp gave in his answer, I was able to solve this one.
Gives output:
Update: The short name for the time zones are not unique. For instance, in both America and Australia there is an "EST", leading timezone_name_from_abbr to sometimes pick the one I don't want to... You can ask date how big the offset is, and that can be used to further match the correct time zone:
如果您设置了时区,则可以使用:http://php.php。 net/manual/en/function.date-default-timezone-get.php
或 http://php.net/manual/en/function.timezone-name-from-abbr.php
If you have timezone set you could use: http://php.net/manual/en/function.date-default-timezone-get.php
Or http://php.net/manual/en/function.timezone-name-from-abbr.php
我最近使用过几次,但我不知道它适用于哪些 Linux 发行版:
也许
timedatectl
更可预测:所以这也很简单:
I've used this a few times recently, but I don't know on which Linux distros this works:
Maybe
timedatectl
is more predictable:so that's pretty easy too: