NightHawk NightHawk - 2 days ago 3
PHP Question

php.ini default timezone vs. date.timezone

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.

Answer

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.