php.ini 默认时区与 date.timezone
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13033407/
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
php.ini default timezone vs. date.timezone
提问by NightHawk
When I use PHP's date()function on two different servers, I get two different results, but both servers should be the same.
当我date()在两个不同的服务器上使用 PHP 的函数时,我得到两个不同的结果,但是两个服务器应该是相同的。
I checked the php.inifile on server #1, where the time is correct, and it looks as follows:
我检查了php.ini服务器 #1 上的文件,时间正确,如下所示:
date/time support enabled
"Olson" Timezone Database Version 0.system
Timezone Database internal
Default timezone America/Chicago
Directive Local Value Master Value
---------------------------------------------------
date.timezone America/Chicago America/Chicago
I checked on server #2 and it looks as follows:
我检查了服务器 #2,它看起来如下:
date/time support enabled
"Olson" Timezone Database Version 0.system
Timezone Database internal
Default timezone UTC
Directive Local Value Master Value
---------------------------------------------------
date.timezone America/Chicago America/Chicago
The only difference I see is the "Default timezone" value.
我看到的唯一区别是“默认时区”值。
The date/time for both servers current display as:
两台服务器当前的日期/时间显示为:
Server #1: 10/23/2012 09:40:39
Server #2: 10/23/2012 14:40:39
I confirmed that both servers use the php.inilocated within /etcand I also searched both web directories for any place the timezone might be overwritten:
我确认两台服务器都使用php.ini位于其中的位置,/etc并且我还在两个 Web 目录中搜索了时区可能被覆盖的任何位置:
grep -r "date_default_timezone_set" *
But in that regard, they both contain the same files with the same settings.
但在这方面,它们都包含具有相同设置的相同文件。
Is "Default timezone" what's causing the 5h difference? If so, how do I correct it?
“默认时区”是什么导致了 5 小时的差异?如果是这样,我该如何纠正?
UPDATE
更新
Loaded configuration files.
加载的配置文件。
Server #2 contains two additional ini files:
服务器 #2 包含两个额外的 ini 文件:
/etc/php.d/snmp.ini
/etc/php.d/apc.ini
php -i results.
php -i 结果。
Server #1:
服务器 #1:
date/time support => enabled
"Olson" Timezone Database Version => 0.system
Timezone Database => internal
Default timezone => America/Chicago
Directive => Local Value => Master Value
date.timezone => America/Chicago => America/Chicago
Server #2:
服务器#2:
date/time support => enabled
"Olson" Timezone Database Version => 0.system
Timezone Database => internal
Default timezone => America/Chicago
Directive => Local Value => Master Value
date.timezone => America/Chicago => America/Chicago
What's interesting to note here is that for some reason the "Default timezone" does not match on server #2 when viewing it via php -iversus phpinfo()on a web page.
这里有趣的是,由于某种原因,“默认时区”在服务器 #2 上php -i与通过phpinfo()网页查看时不匹配。
SOLUTION
解决方案
The problem was with the CMS and its plugins. While server #1 and #2 had the same files and everything, it appears that plugins are not loaded in the same order on each server, which allowed the last plugin loaded to determine the timezone of my script.
问题出在 CMS 及其插件上。虽然服务器 #1 和 #2 具有相同的文件和所有内容,但似乎每个服务器上的插件加载顺序不同,这允许加载的最后一个插件来确定我的脚本的时区。
The reason php -iand phpinfodiffered is because after you use date_default_timezone_set(), it affects what phpinfo()will print.
原因php -i和phpinfo不同是因为在您使用之后date_default_timezone_set(),它会影响phpinfo()将打印的内容。
The fix was the ensure that I'm in the timezone I needed to be in via date_default_timezone_set(). The reason that didn't work for me before I posted this question was because I declared this prior to loading a few required files from the CMS, which probably set the timezone again in there.
解决方法是确保我处于我需要进入的时区 via date_default_timezone_set()。在我发布这个问题之前对我不起作用的原因是因为我在从 CMS 加载一些必需的文件之前声明了这一点,这可能会在那里再次设置时区。
采纳答案by Jason McCreary
date()relies on the date.timezoneINI setting. Since one is Chicago (CT) and the other is UTC, that is your 5 hour difference.
date()依赖于date.timezoneINI 设置。由于一个是芝加哥 (CT),另一个是 UTC,这就是您的 5 小时时差。
I believeas of PHP > 5.2 you should receive:
我相信从 PHP > 5.2 开始,您应该会收到:
PHP Warning: Unknown: It is not safe to rely on the system's timezone settings. You are requiredto use the date.timezone setting or the date_default_timezone_set() function.
PHP 警告:未知:依赖系统的时区设置是不安全的。您需要使用 date.timezone 设置或 date_default_timezone_set() 函数。
I would encourage you to look at the new DateTimeobject or use UTC as advised by SDC.
我鼓励您查看新的DateTime对象或按照SDC 的建议使用 UTC 。
回答by bradym
A few things to check:
需要检查的几件事:
- Check the "Loaded Configuration File" value in your phpinfo() output to make sure you're changing the correct php.ini file. (This has bitten me more than once!)
- When looking at phpinfo() output check the "Additional .ini files parsed" line to see if there are additional ini files being parsed when running via your web server.
- date.timezone can be set via the ini_set function. Do you have any code that does that? Be sure to check any code that may be running due to the auto_prepend ini setting as well.
- 检查 phpinfo() 输出中的“加载的配置文件”值,以确保您正在更改正确的 php.ini 文件。(这让我咬了不止一次!)
- 查看 phpinfo() 输出时,请检查“已解析的其他 .ini 文件”行,以查看在通过 Web 服务器运行时是否正在解析其他 ini 文件。
- date.timezone 可以通过 ini_set 函数设置。你有任何代码可以做到这一点吗?请务必检查由于 auto_prepend ini 设置而可能正在运行的任何代码。
回答by Gizmozine
Here is the code:
这是代码:
[Date]
date.timezone = ('America/New_York')
date.default_latitude = 31.7667
date.default_longitude = 35.2333
date.sunrise_zenith = 90.583333
date.sunset_zenith = 90.583333
Just change date.timezone = ('America/New_York')to your desired timezone.
只需更改date.timezone = ('America/New_York')为您想要的时区。
来源。
回答by aten
Using Ubuntu 14.0 LTS Default timezone in my case was set in:
在我的情况下,使用 Ubuntu 14.0 LTS 默认时区设置为:
/etc/php5/cli/php.ini
/etc/php5/cli/php.ini
I could then verify with:
然后我可以验证:
php -i | grep "timezone"
php -i | grep“时区”
回答by paralaks
If you set the date.timezone value in php.ini but it does not work (ie. local value and master value are different) then you might need to update something like this in your apache configuration file:
如果您在 php.ini 中设置了 date.timezone 值但它不起作用(即本地值和主值不同)那么您可能需要在您的 apache 配置文件中更新如下内容:
<IfModule mod_php5.c>
php_value date.timezone "Europe/Paris"
</IfModule>
To the mod who deleted the above answer before: I have searched hours to find this solution. Don't delete an answer just because you think it is a duplicate answer.
对于之前删除上述答案的模组:我已经搜索了几个小时才能找到此解决方案。不要仅仅因为您认为它是重复的答案就删除它。
回答by Willem van Duijn
http://us.php.net/manual/en/function.date-default-timezone-get.php
http://us.php.net/manual/en/function.date-default-timezone-get.php
The TZ environment takes precedence over the php.ini default value; maybe that's the reason why. You can fix this, by explicitly calling date_default_timezone_set.
TZ 环境优先于 php.ini 默认值;也许这就是原因。您可以通过显式调用 date_default_timezone_set 来解决此问题。

