在 php 中获取服务器时间 - 时区问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3595164/
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
get server time in php - timezone issues
提问by azv
on shell, my server time is (simple date
in bash):
Sun Aug 29 10:37:12 EDT 2010
在 shell 上,我的服务器时间是(date
在 bash 中很简单):Sun Aug 29 10:37:12 EDT 2010
when I run the code php -r "echo date('Y-m-d H:i:s');"
I get: 2010-08-29 10:37:10
当我运行代码时,php -r "echo date('Y-m-d H:i:s');"
我得到:2010-08-29 10:37:10
but when I execute a php script that has echo date('Y-m-d H:i:s');
I get a different time- since php thinks the timezone is UTC (server time is EDT).
但是当我执行一个 php 脚本时,echo date('Y-m-d H:i:s');
我得到了一个不同的时间 - 因为 php 认为时区是 UTC(服务器时间是 EDT)。
My php.ini file has no timezone set, and I wouldnt like to change anything- as I have no idea what affect it would have on other sites that run on this server.
我的 php.ini 文件没有设置时区,我不想更改任何内容 - 因为我不知道它会对在此服务器上运行的其他站点产生什么影响。
Is there a simple way to get the server time- at the timezone that is set for the server?
有没有一种简单的方法可以在为服务器设置的时区获取服务器时间?
采纳答案by Daniel Vandersluis
According to the php.ini directivesmanual page, the timezone isn't set by default if you're using a version prior to 5.3. Then if you take a look at the page for date_default_timezone_get
, it uses the following order for getting the timezone:
根据php.ini 指令手册页,如果您使用的是 5.3 之前的版本,则默认情况下不会设置时区。然后,如果您查看 页面date_default_timezone_get
,它会使用以下顺序获取时区:
* Reading the timezone set using the date_default_timezone_set() function (if any)
* Reading the TZ environment variable (if non empty) (Prior to PHP 5.3.0)
* Reading the value of the date.timezone ini option (if set)
* Querying the host operating system (if supported and allowed by the OS)
UTC is the default if all of those fail, so that's probably a good starting point.
如果所有这些都失败,UTC 是默认值,所以这可能是一个很好的起点。
回答by Artefacto
The server should alwayshave a timezone specified in the php.ini
file. PHP 5.3 even raises notices if that's not the case and the DateTime
extension will throw exceptions if you attempt to build objects without explicitly specifying a timezone (with a DateTimezone
object) or having a default one (either through date.timezone
or date_default_timezone_set
).
服务器应始终在php.ini
文件中指定时区。如果情况并非如此,PHP 5.3 甚至会发出通知,并且DateTime
如果您尝试构建对象而未明确指定时区(使用DateTimezone
对象)或具有默认时区(通过date.timezone
或date_default_timezone_set
),扩展将抛出异常。
By what you're describing, the most likely scenarios are:
根据您的描述,最可能的情况是:
- Different php.ini files are being used in the two scenarios. In the first case, no default timezone is specified, so PHP attempts to infer one from the system-wide timezone of the machine.
- The (second) script calls
date_default_timezone_set('UTC')
or equivalent.
- 这两种情况使用了不同的 php.ini 文件。在第一种情况下,没有指定默认时区,因此 PHP 尝试从机器的系统范围时区推断一个。
- (第二个)脚本调用
date_default_timezone_set('UTC')
或等效项。
回答by pascalbrax
Late for the party, but if you want to set the timezone without edit php.ini, you can put in the php code one of these strings:
聚会迟到了,但如果你想在不编辑 php.ini 的情况下设置时区,你可以在 php 代码中输入以下字符串之一:
date_default_timezone_set('GMT');
date_default_timezone_set('Europe/Zurich');
date_default_timezone_set('America/New_York');
回答by Shaun Hare
Have you tried using date_default_timezone_set() American timezones can be found here http://www.php.net/manual/en/timezones.america.php
您是否尝试过使用 date_default_timezone_set() 可以在此处找到美国时区http://www.php.net/manual/en/timezones.america.php