php.ini默认时区与date.timezone [英] php.ini default timezone vs. date.timezone

查看:332
本文介绍了php.ini默认时区与date.timezone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在两个不同的服务器上使用PHP的date()函数时,会得到两个不同的结果,但是两个服务器应该是相同的.

When I use PHP's date() function on two different servers, I get two different results, but both servers should be the same.

我在时间#正确的服务器#1上检查了php.ini文件,它看起来如下:

I checked the php.ini file on server #1, where the time is correct, and it looks as follows:

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

我检查了服务器#2,它看起来如下:

I checked on server #2 and it looks as follows:

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

我确认两个服务器都使用位于/etc中的php.ini,并且我还在两个Web目录中搜索了可能会覆盖时区的任何地方:

I confirmed that both servers use the php.ini located within /etc and I also searched both web directories for any place the timezone might be overwritten:

grep -r "date_default_timezone_set" *

但是在这方面,它们都包含具有相同设置的相同文件.

But in that regard, they both contain the same files with the same settings.

默认时区"是造成5h差异的原因吗?如果是这样,我该如何纠正?

Is "Default timezone" what's causing the 5h difference? If so, how do I correct it?

更新

已加载的配置文件.

服务器#2包含两个其他ini文件:

Server #2 contains two additional ini files:

/etc/php.d/snmp.ini
/etc/php.d/apc.ini

php -i结果.

服务器#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

服务器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

这里有趣的是,由于某些原因,通过网页上的php -iphpinfo()进行查看时,默认时区"在服务器#2上不匹配.

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 -i versus phpinfo() on a web page.

解决方案

问题出在CMS及其插件.尽管服务器#1和#2具有相同的文件和所有内容,但是似乎插件在每个服务器上的加载顺序不同,这使得最后一个插件可以确定脚本的时区.

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.

php -iphpinfo不同的原因是因为使用date_default_timezone_set()后,它会影响phpinfo()的打印内容.

The reason php -i and phpinfo differed is because after you use date_default_timezone_set(), it affects what phpinfo() will print.

解决方法是确保我处于需要通过date_default_timezone_set()进入的时区.在我发布此问题之前对我不起作用的原因是,我在从CMS加载了一些必需文件之前就声明了这一点,这很可能又在其中设置了时区.

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.

推荐答案

date() 依赖 date.timezone INI设置.由于一个是芝加哥(CT),另一个是UTC,所以您的时差是5小时.

date() relies on the date.timezone INI setting. Since one is Chicago (CT) and the other is UTC, that is your 5 hour difference.

我相信自PHP> 5.2起,您应该会收到:

I believe as of PHP > 5.2 you should receive:

PHP警告:未知:依靠系统的时区是不安全的 设置.您需要 使用date.timezone设置或 date_default_timezone_set()函数.

PHP Warning: Unknown: It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function.

我鼓励您查看新的 DateTime 对象或按照 SDC 的建议使用UTC.

I would encourage you to look at the new DateTime object or use UTC as advised by SDC.

这篇关于php.ini默认时区与date.timezone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆