Home  >  Article  >  Backend Development  >  What to do if an error occurs in the php date function

What to do if an error occurs in the php date function

藏色散人
藏色散人Original
2021-07-02 09:21:142589browse

Solution to php date function error: 1. Add date.timezone item in php.ini; 2. Add "date_default_timezone_set("PRC");" to the page; 3. Set the time zone in the header of the page.

What to do if an error occurs in the php date function

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

What should I do if the php date function fails?

Solution to the error using date() in PHP

A warning appeared during PHP debugging:

It is not safe to The solution to rely on the system is actually caused by incorrect time zone settings. This article provides 3 methods to solve this problem.

In fact, starting 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 , the date.timezone option is turned off by default. No matter what PHP command is used, it is Greenwich Mean Time. However, if part of the time function is not set in PHP5.3, this error will be forcibly thrown.

PS: Since most people now use VPS/cloud hosts, this situation is even more likely to occur if they need to configure their own environment.

Suggestion: If you are not familiar with the PHP environment, use the more mature one-click installation package.

Method 1:

(The best method) Find the date.timezone item in php.ini and set date.timezone = "Asia/ Shanghai", restart the environment and it will be ok.

Method 2:

When you need to use these time functions, add date_default_timezone_set("PRC");

Method 3:

Add setting time zone in the header of the pageini_set('date.timezone','Asia/Shanghai');

错误代码:
Warning: 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 ‘Asia/Chongqing’ for ‘CST/8.0/no DST’ instead

Recommended learning: "PHP Video Tutorial

The above is the detailed content of What to do if an error occurs in the php date function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn