Summary of Linux php time zone setting method_PHP tutorial

WBOY
Release: 2016-07-13 10:48:52
Original
1231 people have browsed it

The default time zone in PHP is 8 hours related to China. Now I will introduce to you how to solve the 8-hour PHP time difference between Linux and Windows systems. I hope it will be helpful to all students.

The available values ​​in mainland China are: Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (in order Chongqing, Shanghai, Urumqi)
Available in Hong Kong and Taiwan: Asia/Macao, Asia/Hong_Kong, Asia/Taipei (Macau, Hong Kong, Taipei in order)
And Singapore: Asia/Singapore

Other available values ​​are: Etc/GMT-8, Singapore, Hongkong, PRC


1. Set the time zone in Linux, taking CentOS5.5 as an example:

The code is as follows Copy code
 代码如下 复制代码

cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

2. Set time zone in MySQL

Edit the MySQL configuration file: my.cnf/my.ini, add:
 代码如下 复制代码

default-time-zone = '+8:00'

under [mysqld]
The code is as follows Copy code

default-time-zone = '+8:00'

In fact, if the time zone of the OS where MySQL is located has been set correctly, you do not need to set the time zone in MySQL, because when the MySQL service starts, it will automatically adopt the time zone of the OS.

3. Set time zone in PHP

 代码如下 复制代码

date.timezone = "Asia/Shanghai"

In the PHP configuration file PHP.ini:

The code is as follows Copy code
date.timezone = "Asia/Shanghai"

After setting up, it is best to restart the system.
 代码如下 复制代码

 ini_set('date.timezone', 'Asia/Shanghai'); 

 date_default_timezone_set('Asia/Shanghai');

If you purchased the web space, you do not have permission to configure the time zone in Linux PHP MySQL. That's easy to do, you just need to set the time zone corresponding to the OS in the PHP script, because MySQL uses the OS time zone by default. Assuming that the time zone of the OS is 'Asia/Shanghai', use one of the following two statements:

The code is as follows Copy code
ini_set('date.timezone', 'Asia/Shanghai'); date_default_timezone_set('Asia/Shanghai');
 代码如下 复制代码

date.timezone = PRC

 代码如下 复制代码

 
1 date_default_timezone_set('Asia/Shanghai');//'Asia/Shanghai'   亚洲/上海

2 date_default_timezone_set('Asia/Chongqing');//其中Asia/Chongqing'为“亚洲/重庆”

3 date_default_timezone_set('PRC');//其中PRC为“中华人民共和国”

4i ni_set('date.timezone','Etc/GMT-8');

5 ini_set('date.timezone','PRC');

6 ini_set('date.timezone','Asia/Shanghai');

7 ini_set('date.timezone','Asia/Chongqing');

Second, in windows we need to set the time zone in PHP.INI
The code is as follows Copy code
date.timezone = PRC
Set time zone in code
The code is as follows Copy code
1 date_default_timezone_set('Asia/Shanghai');//'Asia/Shanghai' Asia/Shanghai 2 date_default_timezone_set('Asia/Chongqing');//Asia/Chongqing' is "Asia/Chongqing" 3 date_default_timezone_set('PRC');//where PRC is "People's Republic of China" 4i ni_set('date.timezone','Etc/GMT-8'); 5 ini_set('date.timezone','PRC'); 6 ini_set('date.timezone','Asia/Shanghai'); 7 ini_set('date.timezone','Asia/Chongqing');

Any of the above seven methods can meet our needs.

When initializing the system, add

ini_set('date.timezone','Asia/Shanghai');
or
date_default_timezone_set("PRC");
The code is as follows
 代码如下 复制代码

ini_set('date.timezone','Asia/Shanghai'); 

date_default_timezone_set("PRC"); 

Copy code

This will solve the time zone difference of 8 http://www.bkjia.com/PHPjc/632744.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/632744.html
TechArticle
The default time zone in php is related to China for 8 hours. Let me introduce to you the difference between linux and windows system. How to solve the 8-hour time difference in PHP. I hope it will be helpful to all students. Within the mainland...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!