Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 687
Nick Avatar asked Apr 25 '12 01:04

Nick


People also ask

What is the use of date_default_timezone_set ()?

The date_default_timezone_set() function sets the default timezone used by all date/time functions in the script.

What timezone is UTC for PHP?

The default timezone for PHP is UTC regardless of your server's timezone. This is the timezone used by all PHP date/time functions in your scripts. See PHP's list of supported timezones to find the names of all possible timezones you can use for the date.

How does PHP determine timezone?

The date_default_timezone_get() function returns the default timezone used by all date/time functions in the script.

What is PHP date function?

The date() function formats a local date and time, and returns the formatted date string.


2 Answers

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');
like image 114
John Conde Avatar answered Oct 20 '22 20:10

John Conde


It's not an exception, it's a warning that is probably popping up now because your error reporting settings on the new machine are different from the old one.

I would suggest to follow the suggestion in the warning, and use date_default_timezone_set() to set a time-zone in the scripts where you need it.

like image 36
jeroen Avatar answered Oct 20 '22 18:10

jeroen