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.
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.
A few things to check:
Here is the code:
Just change
date.timezone = ('America/New_York')
to your desired timezone.Source.
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"
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:
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.
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:
I would encourage you to look at the new DateTime object or use UTC as advised by SDC.