How to fix warning from date() in PHP"
Solution 1
Try to set date.timezone
in php.ini
file. Or you can manually set it using ini_set()
or date_default_timezone_set()
.
Solution 2
You need to set the default timezone smth like this :
date_default_timezone_set('Europe/Bucharest');
More info about this in http://php.net/manual/en/function.date-default-timezone-set.php
Or you could use @
in front of date to suppress the warning however as the warning states it's not safe to rely on the servers default timezone
Solution 3
You could also use this:
ini_alter('date.timezone','Asia/Calcutta');
You should call this before calling any date function. It accepts the key as the first parameter to alter PHP settings during runtime and the second parameter is the value.
I had done these things before I figured out this:
- Changed the PHP.timezone to "Asia/Calcutta" - but did not work
- Changed the lat and long parameters in the ini - did not work
- Used
date_default_timezone_set("Asia/Calcutta");
- did not work - Used
ini_alter()
- IT WORKED - Commented
date_default_timezone_set("Asia/Calcutta");
- IT WORKED - Reverted the changes made to the PHP.ini - IT WORKED
For me the init_alter()
method got it all working.
I am running Apache 2 (pre-installed), PHP 5.3 on OSX mountain lion
Solution 4
This just happen to me because in the php.ini the date.timezone was not set!
;date.timezone=Europe/Berlin
Using the php date() function triggered that warning.
Nano HE
A newbie, China based; And I am new to programming. Work mostly with C#, C++, Perl & Php; Using OS: winxp; C#/C++: Visual Studio 2005/2008; Perl: Active Perl 5.10.1 + Komodo Edit 6; Php: NetBeans IDE 6.7 + XAMPP 1.7.3 Thanks for reading and for the replies! ;) I'm Reading Magic Tree House
Updated on March 13, 2020Comments
-
Nano HE about 4 years
I am using XAMPP(PHP Version 5.3.1) on winxp. When I try to call time() or date() function on my localhost. It will show warning message as this,
Severity: Warning
Message: 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 'UTC' for '8.0/no DST' instead
Filename: helpers/date_helper.php
How can I disable the warning? Thanks.
-
Nano HE about 13 yearsI found it. Done and works now.
date.timezone = Asia/Shanghai
-
Mirko about 11 yearsThis is the only method that works
-
Frank about 11 yearsThis answer is not working in my case. Check-Here: stackoverflow.com/questions/15807087/… How do i use
ini_set()
. -
Admin over 10 yearsThanks for several options and a link to the manual. However nor the manual nor the warning are very clear. What is not safe about it. I want to use the local time. And when I travel with my computer and I change my timezone I want it to use that instead of some stale version in php.ini. There is a reason we have a system time no?
-
Poelinca Dorin over 10 yearsIf you travel around the globe, your OS timezone does change ? If so you can check this out and set it properly stackoverflow.com/questions/4700156/… if not, then you could do a geoip lookup but you'll run into different problems (each request a user makes, your server makes another one, so you need to store the value and so on). A server is not meant to travel around the globe is it ?
-
Admin over 10 yearssure, servers are usually stationary, even though they don't need to be necessarily. However I'm not only using php for server applications, so yeah, when I travel I set my OS timezone to see the correct time. And even though there are lots of pages on the internet about this warning, no one explains what's not safe about it...
-
Poelinca Dorin over 10 yearsMost of php applications are websites, and the majority of them are hosted on shared servers ... so the system time is not what most of developers expect for their targeted audience. p.s. this is just my common sense :)
-
Alex Pliutau over 10 years@Frank, you can
date_default_timezone_set('Europe/Minsk');
-
ChrisR about 10 yearsJust a heads up for anybody wondering but
ini_alter
is just a lesser known alias ofini_set
-
Hamman Samuel over 7 yearsList of valid values for
date.timezone
php.net/manual/en/timezones.php