PHP - date() vs. date.timezone / date_default_timezone_set()

Nick picture Nick · Apr 25, 2012 · Viewed 22.9k times · Source

I've just got a new computer, and I've been setting up PHP/MySQL/databases etc... I think I'm just about there, except it's thrown this curveball. My login script was working fine, but now it's spitting the following warning (which messes up the JSON).

Warning: date() [function.date]: 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. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Antarctica/Macquarie' for 'EST/10.0/no DST' instead in .../php/login.php on line 47

My code obviously uses date() and is working in the live version and on the old machine. I get two warnings for the following two lines of code:

$date = date("ymd");

$this_year = date("y");

My research (see here) suggests that the behaviour of these functions depends on php.ini .

So should I change php.ini on the new machine, or am I using some kind of deprecated method, and should I ditch date() altogether?

Thanks.

Answer

John Conde picture John Conde · Apr 25, 2012

You don't need to change the php.ini file if you use date_default_timezone_set(). Just set it to the timezone you will be working in.

Something like this should go in a config file or on the page where you're working with dates (if it is only one page):

date_default_timezone_set('America/Los_Angeles');