Home >Backend Development >PHP Tutorial >How to complete the time zone setting in PHP?
In the previous article, I brought you "Take you to understand the error types and error levels of PHP", which introduced the error types and error levels in PHP in detail. In this article we Let’s take a look at how we should set the time zone in PHP. I hope it will be helpful to everyone!
In our daily life, time is very important and essential. The same is true in PHP. In our daily development, sometimes we use When time needs to be stored to record something, how should we set the time?
In PHP, you can get the date and time through the date time function. The date and time functions in PHP are set through the server time. The server time defaults to Greenwich Time.
If you want to change the time in PHP to local time, you need to change the time zone settings in the PHP language. There are three ways to set the language time zone in PHP. Next, let’s take a look:
Configure in the configuration file
The configuration file in PHP is the php.ini file. As for how to find this file, I mentioned in the previous article "How to upload files in PHP? You’ll understand after reading it! " was mentioned in "If you are interested, you can click to check it out. After finding php.ini, search for date.timezone in the file and find the following result:
Asia/Shangha represents the time zone, also called the time zone identifier. There are many such time zone identifiers. Here are some commonly used time zone identifiers:
Asia/Shanghai —— Shanghai
Asia/Chongqing —— Chongqing
Asia /Hong_Kong —— Hong Kong
Asia/Macao —— Macao
Asia/Taipei —— Taipei
PRC ——China time zone
It should be noted that , when we complete the time zone setting, we need to restart the server for the lost settings to take effect.
Use date_default_timezone_set()
function to set the time zone
in In PHP, the date_default_timezone_set() function can set a default time zone for all date functions. Its syntax format is as follows:
date_default_timezone_set(时区标识符)
The example is as follows:
<?php date_default_timezone_set('Asia/Hong_Kong'); echo '香港的当前时间为:'.date('Y-m-d H:i:s',time()).'<br>'; date_default_timezone_set('Asia/Macao'); echo '澳门的当前时间为:'.date('Y-m-d H:i:s',time()).'<br>'; date_default_timezone_set('Asia/Shanghai'); echo '上海的当前时间为:'.date('Y-m-d H:i:s',time()); ?>
Output result:
#It can be seen from the above results that a time zone can be set through the date_default_timezone_set() function.
Use ini_set()
Function to set the time zone
ini_set(待修改的选项, 该选项新的值)The example is as follows:
<?php ini_set('date.timezone', 'GMT'); echo '当前的格林尼治时间为:'.date('Y-m-d H:i:s',time()).'<br>'; ini_set('date.timezone', 'Asia/Hong_Kong'); echo '香港的当前时间为:'.date('Y-m-d H:i:s',time()).'<br>'; ini_set('date.timezone', 'Asia/Shanghai'); echo '上海的当前时间为:'.date('Y-m-d H:i:s',time()); ?>Output result:
PHP Video Tutorial" to learn more about PHP knowledge.
The above is the detailed content of How to complete the time zone setting in PHP?. For more information, please follow other related articles on the PHP Chinese website!