Home >Backend Development >PHP Tutorial >Solve the problem of 'It is not safe to rely on the system's timezone settings” in PHP, relytimezone_PHP tutorial
If you use PHP5.3 or above, if php is not configured correctly. ini will cause an error with the PHP date function. Many old PHP programming tutorials did not mention this problem in the past, so many readers will feel confused. The author below will tell you three methods to solve this problem.
"PHP 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 missed the timezone identifier.
We selected 'UTC' for '8.0/no DST' instead in"
In fact, from PHP 5.1.0, when using functions such as date(), if the timezone is set incorrectly, E_NOTICE or E_WARNING information will be generated every time the time function is called. And in PHP 5.1.0, the date.timezone option is turned off by default. No matter what PHP command is used, it is Greenwich Mean Time. However, in PHP 5.3, it seems that this will be forcibly thrown out if it is not set. Wrong, to solve this problem, just localize it.
1. Use date_default_timezone_set() in the header to set date_default_timezone_set('PRC'); //Eastern Eighth Time Zone echo
date('Y-m-d H:i:s');
2. Use ini_set('date.timezone','Asia/Shanghai');
in the header of the page3. Modify php.ini (if it is a windows system, then the file is in the C drive, Windows directory, if the system is installed on the C drive). Use Notepad to open php.ini and search for date.timezone. Remove the semicolon in front and change it to: date.timezone = PRC
Just restart the http service (such as apache2 or iis, etc.)!
You can choose any one of the above three methods. The editor recommends using the third method without adding extra code every time.