In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, the editor will share with you the relevant knowledge about how to change the time zone in php. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
The method to change the time zone in php: 1, search the date.timezone in the configuration file and set the time zone; 2, use the ini_set () function to set the time zone; 3, use the date_default_timezone_set () function to set the time zone.
This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer
How do I change the time zone in php?
In PHP, date and time are obtained through date and time functions. The date and time functions depend on the time setting of the server. The default time setting of the server is Greenwich time (00:00 zone time). If the time is not set to a specific time zone, the time in the zero time zone obtained by the PHP function is 8 hours less than Beijing time.
Division of time zone
The world is divided into 24 time zones, each of which has its own local time. At the same time, the local time of each time zone differs by 1: 23 hours. For example, the difference between London local time and Beijing local time is 8 hours.
In the field of international radio communications, a unified time is used, called Universal coordinated time (Universal Time Coordinated,UTC), and UTC is the same as Greenwich mean time (Greenwich Mean Time,GMT).
Time zone settings in PHP
Because PHP5.0 overrides the data () function, the current date-time function is eight hours less than the system time. The PHP language defaults to the standard Greenwich mean time (that is, the 00:00 zone), so to get the local current time, you must change the time zone setting in the PHP language.
There are three ways to set the time zone in PHP, which are described below.
1) set in the configuration file
The configuration file for PHP is the php.ini file in the PHP installation directory, find it and open it. Then search for date.timezone in the file, and we will see a piece of information like this:
[Date]; Defines the default timezone used by the date functions; http://php.net/date.timezone;date.timezone =
Where; date.timezone = preceding; the same function as / / in PHP, both mean comments. Here, we need to remove this; and fill in the corresponding time zone identifier after the = in this sentence, as shown below:
[Date]; Defines the default timezone used by the date functions; http://php.net/date.timezonedate.timezone = Asia/Shangha
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 their meanings:
Asia/Shanghai-Shanghai Asia/Chongqing-Chongqing Asia/Urumqi-Urumqi Asia/Hong_Kong-Hong Kong Asia/Macao-Macau Asia/Taipei-Taipei Asia/Singapore-Singapore PRC-China time zone
Tip: in China, we can set the time zone to "Asia/Shanghai or PRC". Remember to restart the server after the setting is successful!
[example] Let's use a piece of code to verify whether our settings are in effect.
The running results are as follows:
2020-03-26 09:56:52
If the running result is equal to the local time of our computer, it means that we have set it up successfully.
2) use ini_set () function to set the time zone
The ini_set () function in PHP sets the value of the specified configuration option, which remains at the new value when the script runs and is restored at the end of the script. The syntax format of the function is as follows:
Ini_set ($varname, $newvalue)
Where $varname is the configuration option to be modified, and $newvalue is the new value of the configuration option.
Tip: the ini_set () function does not modify all configuration options. You can view the configuration options that can be modified by visiting "https://www.php.net/manual/zh/ini.list.php"".
[example] use the ini_set () function to set the time zone.
The running results are as follows:
Current GMT: 2020-03-26 02:37:31 Urumqi current time: 2020-03-26 08:37:31 Shanghai current time: 2020-03-26 10:37:31
3) use date_default_timezone_set () function to set the time zone
The date_default_timezone_set () function in PHP sets a default time zone for all time and date functions in the script, and its syntax format is as follows:
Date_default_timezone_set ($timezone_identifier)
The parameter $timezone_identifier is a time zone identifier, such as UTC (Greenwich mean time) or Europe/Lisbon (Portugal).
Since PHP5.1.0 (this version of the date-time function has been rewritten), every call to the date-time function generates an E_NOTICE-level error message if the time zone is illegal, and an E_STRICT-level message if the system settings or TZ environment variables are used.
[example] use the date_default_timezone_set () function to set the time zone.
The running results are as follows:
The current time in Urumqi is: 2020-03-26 08:35:59 in Portugal: 2020-03-26 02:35:59 in Shanghai the current time is: 2020-03-26 10:35:59 is all the content of the article "how to change the time zone in php". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.