How to change time zone in php

藏色散人
Release: 2023-03-17 14:48:02
Original
2788 people have browsed it

How to modify the time zone in PHP: 1. Set the default time zone to East 8 in the configuration file; 2. Set the time zone through the code "date_default_timezone_set('Asia/Shanghai');"; 3. In the PHP page header Set "date_default_timezone_set('PRC');" in

How to change time zone in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to change the time zone in php?

PHP time zone settings, PHP time issues

Many web projects are related to time, and some are very closely related to time! Some time ago, I made a 15-day weather forecast query (http://tqybw.net) project. The function and display are very closely related to the time setting. Now I will set the PHP time zone and explain the PHP time issue in detail. Students in need can learn from it. Down!

The time obtained from the time function "time()" in the PHP function may not be the standard Beijing time! The reason is because: the function int time (void) in PHP returns the number of seconds since the Unix epoch (00:00:00 Greenwich Mean Time on January 1, 1970) to the current time.

Each region has its own local time. On the Internet and in radio communications, the problem of time conversion is particularly prominent. The entire earth is divided into twenty-four time zones, each with its own local time. In international radio or network communication situations, for the sake of unification, a unified time is used, called Universal Time Coordinated (UTC), which is a global standard time set by the world time standard. UTC was originally also known as Greenwich Mean Time (GMT, Greenwich Mean Time), which is the same as the local time in London, UK.

PHP’s default time zone setting is UTC time, and Beijing is located in the East Eighth District of the time zone, eight hours ahead of UTC. Therefore, when using functions such as time() in PHP to obtain the current time, the time obtained is always wrong, showing that it is eight hours different from Beijing time. If you want to display Beijing time correctly, you need to modify the default time zone settings, which can be done in the following two ways.

If you are using an independent server and have permission to modify the configuration file, setting the time zone can be completed by modifying the date.timezone attribute in php.ini. We can set the value of this attribute to one of "Asia/Shang", "Asia/Chongqing", "Etc/GMT-8" or PRC, etc., and then the current time obtained in the PHP script is Beijing time. Modify the PHP configuration file as follows:

1.date.timezone = Etc/GMT-8

//Set the default time zone in the configuration file to East 8 Zone (Beijing time)

If you are using a shared server and do not have permission to modify the configuration file php.ini, and the PHP version is above 5.1.0, you can also call the date_default_timezone_set() function before outputting the time. Time zone. This function needs to provide a time zone identifier as a parameter, which is the same as the value of the date.timezone attribute in the configuration file. The use of this function is as follows:

2. Set the time zone in the code

date_default_timezone_set('Asia/Shanghai');//'Asia/Shanghai' 亚洲/上海
date_default_timezone_set('Asia/Chongqing');//其中Asia/Chongqing'为“亚洲/重庆”
date_default_timezone_set('PRC');//其中PRC为“中华人民共和国”
Copy after login

Note: Any of the above 3 methods can meet our needs.

3.echo date('Y-m-d H:i:s', time()); //The current time output is Beijing time

In php.ini , the default is date.timezone = UTC. Modify to China time zone and modify it to date.timezone = PRC. If written directly in GMT format, it is date.timezone = Etc/GMT 8.

In addition, it can also be set in the PHP page header.

date_default_timezone_set('PRC');
Copy after login

Test:

echo date('Y-m-d H:i:s');
Copy after login

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to change time zone in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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