Home > Backend Development > PHP Problem > What are the PHP time zone conversion functions?

What are the PHP time zone conversion functions?

青灯夜游
Release: 2023-03-16 09:06:01
Original
1729 people have browsed it

There are 2 functions: 1. ini_set(), the syntax is "ini_set('date.timezone','identifier)"; 2. date_default_timezone_set(), the syntax is "date_default_timezone_set('identifier')".

What are the PHP time zone conversion functions?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

There are two php time zone conversion functions :

  • ini_set() function

  • date_default_timezone_set() function

1. Use the ini_set() function to set the time zone

The ini_set() function in PHP can set the time zone by setting the value of the "date.timezone" configuration option.

ini_set('date.timezone', '时区标识符')
Copy after login

There are many time zone identifiers, which can be viewed by visiting "https://www.php.net/manual/zh/timezones.php". Here are some commonly used time zone identifiers And its meaning:

  • Asia/Shanghai —— Shanghai

  • Asia/Chongqing —— Chongqing

  • Asia/Urumqi —— Urumqi

  • Asia/Hong_Kong —— Hong Kong

  • Asia/Macao —— Macao

  • Asia/Taipei - Taipei

  • Asia/Singapore - Singapore

  • PRC - China Time Zone

Tip: If you are in China, we can set the time zone to "Asia/Shanghai or PRC"!

[Example] Use the ini_set() function to set the time zone

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
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/Urumqi&#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

What are the PHP time zone conversion functions?

2. Use the date_default_timezone_set() function to set the time zone

The date_default_timezone_set() function in PHP can set a default time zone for all time and date functions in the script. The syntax format is as follows:

date_default_timezone_set($timezone_identifier)
Copy after login

The parameter $timezone_identifier is the time zone identifier, such as UTC ( Greenwich Mean Time) or Europe/Lisbon (Portugal).

Since PHP5.1.0 (the date and time function has been rewritten in this version), if the time zone is illegal, each call to the date and time function will generate an E_NOTICE level error message. If you use the system Setting the or TZ environment variable will also generate E_STRICT level information.

[Example] Use the date_default_timezone_set() function to set the time zone

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
date_default_timezone_set(&#39;Asia/Urumqi&#39;);
echo &#39;乌鲁木齐的当前时间为:&#39;.date(&#39;Y-m-d H:i:s&#39;,time()).&#39;<br>&#39;;
date_default_timezone_set(&#39;Europe/Lisbon&#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

What are the PHP time zone conversion functions?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are the PHP time zone conversion functions?. 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