How to complete the time zone setting in PHP?

WBOY
Release: 2023-04-10 18:02:02
Original
2811 people have browsed it

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!

How to complete the time zone setting in PHP?

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.

Time zone settings in PHP

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:

How to complete the time zone setting in PHP?

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(时区标识符)
Copy after login

The example is as follows:

<?php
    date_default_timezone_set(&#39;Asia/Hong_Kong&#39;);
    echo &#39;香港的当前时间为:&#39;.date(&#39;Y-m-d H:i:s&#39;,time()).&#39;<br>&#39;;
    date_default_timezone_set(&#39;Asia/Macao&#39;);
    echo &#39;澳门的当前时间为:&#39;.date(&#39;Y-m-d H:i:s&#39;,time()).&#39;<br>&#39;;
    date_default_timezone_set(&#39;Asia/Shanghai&#39;);
    echo &#39;上海的当前时间为:&#39;.date(&#39;Y-m-d H:i:s&#39;,time());
?>
Copy after login

Output result:

How to complete the time zone setting in PHP?

#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

##PHP The ini_set() function can set the value of a specified configuration option. It will maintain the new value while the script is running and restore it when the script ends. The syntax format of the function is as follows:

ini_set(待修改的选项, 该选项新的值)
Copy after login

The example is as follows:


<?php
    ini_set(&#39;date.timezone&#39;, &#39;GMT&#39;);
    echo &#39;当前的格林尼治时间为:&#39;.date(&#39;Y-m-d H:i:s&#39;,time()).&#39;<br>&#39;;
    ini_set(&#39;date.timezone&#39;, &#39;Asia/Hong_Kong&#39;);
    echo &#39;香港的当前时间为:&#39;.date(&#39;Y-m-d H:i:s&#39;,time()).&#39;<br>&#39;;
    ini_set(&#39;date.timezone&#39;, &#39;Asia/Shanghai&#39;);
    echo &#39;上海的当前时间为:&#39;.date(&#39;Y-m-d H:i:s&#39;,time());
?>
Copy after login
Output result:


How to complete the time zone setting in PHP?

Listed by Example It can be seen that when the script is restored and a new option is reconfigured, the set time zone option will retain the new value.


If you are interested, you can click on "

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!