Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem that PHP got the wrong time through date () function

Shulou Source: shulou.com Published: 2022-06-03 18:54:02 09月13日 Update

This article mainly explains "how to solve the problem that PHP got the wrong time through date () function". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to solve the problem that PHP got the wrong time through the date () function.

PHP got the time error through the date () function

To get straight to the topic, after obtaining the time through the date () function, you will find that the hour time is wrong, 8 hours less than the current time, whether using H or h, this is because the current acquisition is Greenwich time, the difference between Beijing time and Beijing time is 8 hours.

The solution is as follows:

1. Modify php.ini configuration file

Open the php.ini file and find it in the root directory of the php configuration. Date.timezone, delete the semicolon and change it to date.

Timezone = PRC. Save and restart the Apahce service (sometimes there will be problems with restart, just stop and then start).

2. Add the date_default_timezone_set (timezone_identifier) function

What this function means is to set the current time zone, initialize the time, and add this sentence before the header or get time statement.

For example:

Date_default_timezone_set (PRC)

Echo date ("Y-m-d H-i-s")

The parameters are set as follows:

Date_default_timezone_set (PRC)-set to Beijing time, PRC represents the people's Republic of China

Date_default_timezone_set ('Asia/Shanghai')-set to Shanghai time, and' Chongqi' (Chongqing) and 'Urumqi' (Urumqi) are also available on the mainland.

Available in Hong Kong and Taiwan: Asia/Macao, Asia/Hong_Kong, Asia/Taipei

(followed by Macao, Hong Kong and Taipei)

Singapore area: Asia/Singapore (I don't seem to see Beijing)

This function will always return True, but be sure to enter timezone_identifier, otherwise an error will be reported.

Here are all the time zone identifiers: (for your reference)

CET

CST6CDT

Cuba

EET

Egypt

Eire

EST

EST5EDT

Etc/GMT

Etc/GMT+0

Etc/GMT+1

Etc/GMT+10

Etc/GMT+11

Etc/GMT+12

Etc/GMT+2

Etc/GMT+3

Etc/GMT+4

Etc/GMT+5

Etc/GMT+6

Etc/GMT+7

Etc/GMT+8

Etc/GMT+9

Etc/GMT-0

Etc/GMT-1

Etc/GMT-10

Etc/GMT-11

Etc/GMT-12

Etc/GMT-13

Etc/GMT-14

Etc/GMT-2

Etc/GMT-3

Etc/GMT-4

Etc/GMT-5

Etc/GMT-6

Etc/GMT-7

Etc/GMT-8

Etc/GMT-9

Etc/GMT0

Etc/Greenwich

Etc/UCT

Etc/Universal

Etc/UTC

Etc/Zulu

Factory

GB

GB-Eire

GMT

GMT+0

GMT-0

GMT0

Greenwich

Hongkong

HST

Iceland

Iran

Israel

Jamaica

Japan

Kwajalein

Libya

MET

MST

MST7MDT

Navajo

NZ

NZ-CHAT

Poland

Portugal

PRC

PST8PDT

ROC

ROK

Singapore

Turkey

UCT

Universal

UTC

W-SU

WET

At this point, I believe you have a deeper understanding of "how to solve the problem that PHP got the wrong time through the date () function". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Time function error problem hour Beijing content region file method time zone learning configuration wrong practical deeper representative interest semicolon parameter Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Tech Info Shulou Technology Linux Microsoft