Home > Backend Development > PHP Problem > What should I do if the current time obtained by php is incorrect?

What should I do if the current time obtained by php is incorrect?

藏色散人
Release: 2023-03-08 20:42:02
Original
2154 people have browsed it

Solution: 1. Insert the "date_default_timezone_set(PRC);" statement before the added time; 2. Pass "date("Y-m-d H:i:s", time() 8*60*60) ";" method handles the obtained time; 3. Open the php.ini configuration file, find the ";date.timezone =" line, remove the ";" and change it to "date.timezone = PRC", save the file, and restart Apache.

What should I do if the current time obtained by php is incorrect?

The operating environment of this article: windows7 system, PHP8 version, DELL G3 computer

The problem of inaccurate current time obtained by PHP Solution

This article mainly introduces the solution to the problem of inaccurate current time obtained by PHP. The article introduces it in great detail through sample code, which has certain reference for everyone's study or work. Value, friends in need can refer to

Starting from php5.1.0, the date.timezone option has been added to php.ini, and it is turned off by default, which is the displayed time (no matter what php command is used) The difference between Greenwich Mean Time and our time (Beijing time) is exactly 8 hours.

About timezone, the available values ​​in mainland China are: Asia/Chongqing, Asia/Shanghai, Asia/Urumqi, PRC (in order Chongqing, Shanghai, Urumqi, People's Republic of China) available in Hong Kong and Taiwan: Asia/Macao, Asia/Hong_Kong, Asia/Taipei (Macau, Hong Kong, Taipei in order) and Singapore: Asia/Singapore Others: Etc/GMT-8, Singapore, Hongkong

Solution 1:

Insert before adding the time: CODE: (It seems that there is no need to add) date_default_timezone_set(PRC);

Solution two:

echo date("Y-m-d H:i:s", time()+8*60*60);
Copy after login

Solution three (modify the php.ini file):

Find the line ";date.timezone =" in php.ini and change " ;" Remove it and change it to "date.timezone = PRC" (PRC: People's Republic of China People's Republic of China). Restart Apache and the problem is solved.

Attachment: date time format

date("Y-m-d H:i:s"); Displayed format: year-month-day Hours: Minutes: Seconds

Related time parameters:

a - "am" 或是 "pm"
A - "AM" 或是 "PM"
d - 几日,二位数字,若不足二位则前面补零; 如: "01" 至 "31"
D - 星期几,三个英文字母; 如: "Fri"
F - 月份,英文全名; 如: "January"
h - 12 小时制的小时; 如: "01" 至 "12"
H - 24 小时制的小时; 如: "00" 至 "23"
g - 12 小时制的小时,不足二位不补零; 如: "1" 至 12"
G - 24 小时制的小时,不足二位不补零; 如: "0" 至 "23"
i - 分钟; 如: "00" 至 "59"
j - 几日,二位数字,若不足二位不补零; 如: "1" 至 "31"
l - 星期几,英文全名; 如: "Friday"
m - 月份,二位数字,若不足二位则在前面补零; 如: "01" 至 "12"
n - 月份,二位数字,若不足二位则不补零; 如: "1" 至 "12"
M - 月份,三个英文字母; 如: "Jan"
s - 秒; 如: "00" 至 "59"
S - 字尾加英文序数,二个英文字母; 如: "th","nd"
t - 指定月份的天数; 如: "28" 至 "31"
U - 总秒数
w - 数字型的星期几,如: "0" (星期日) 至 "6" (星期六)
Y - 年,四位数字; 如: "1999"
y - 年,二位数字; 如: "99"
z - 一年中的第几天; 如: "0" 至 "365"
Copy after login

[Recommended: PHP Video Tutorial]

The above is the detailed content of What should I do if the current time obtained by php is incorrect?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template