Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php.ini default timezone vs. date.timezone

Tags:

timezone

php

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.

like image 442
NightHawk Avatar asked Oct 23 '12 15:10

NightHawk


2 Answers

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.

like image 111
Jason McCreary Avatar answered Oct 04 '22 02:10

Jason McCreary


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.
like image 35
bradym Avatar answered Oct 04 '22 02:10

bradym