php.ini default timezone vs. date.timezone

2019-02-08 14:52发布

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

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

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

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.

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

UPDATE

Loaded configuration files.

Server #2 contains two additional ini files:

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

php -i results.

Server #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:

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

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.

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

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.

标签: php timezone
6条回答
太酷不给撩
2楼-- · 2019-02-08 15:11

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.

查看更多
相关推荐>>
3楼-- · 2019-02-08 15:13

A few things to check:

  1. 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!)
  2. 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.
  3. 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.
查看更多
一夜七次
4楼-- · 2019-02-08 15:18

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.

Source.

查看更多
Summer. ? 凉城
5楼-- · 2019-02-08 15:20

Using Ubuntu 14.0 LTS Default timezone in my case was set in:

/etc/php5/cli/php.ini

I could then verify with:

php -i | grep "timezone"

查看更多
狗以群分
6楼-- · 2019-02-08 15:23

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:

<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.

查看更多
劫难
7楼-- · 2019-02-08 15:30

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

I believe as of PHP > 5.2 you should receive:

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.

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

查看更多
登录 后发表回答