About the time zone that's something php study notes

Science what is the time zone

As we all know, the earth around the sun but also rotation, so the same time in different situations in different parts of the received solar radiation, so some areas are sunrise, sunset some areas, and some areas may be the night.

Since different times in different regions on the planet, and that there must be a uniform time scale in order to facilitate cultural and technological exchange it, or we say are local time, this not a mess?

There is a problem we must solve the problem, we must unify different times in different parts of the standard time, and the premise of a unified standard time is local time to figure out how to time differences around the world, and where are interchangeable.

The original standard time is GMT , with the development requires precise timing, has been replaced by a new time standard, the current standard time is UTC .

Now, with a unified standard time, time in different areas can be converted into a unified converted to the local time UTC, never at different times in the same time does not appear!

Example:

  • (Beijing Time UTC+8):2019-05-30 13:30:00
  • World Time ( UTC):2019-05-30 05:30:00
  • Eastern Time ( UTC-5):2019-05-30 00:30:00

Greenwich Mean Time

Greenwich Mean Time (also known as Greenwich Mean Time or GMT , English is a Greenwich mean time , short GMT ), noon Greenwich Mean Time is when the sun crosses the Greenwich meridian time (that is, when GMT) time.

Greenwich is a small town in London, it is the geographical longitude of a starting point, the prime meridian longitude where is zero degrees longitude, time zone is zero zone .

We know that one day total 24hours, corresponding to the world's 24time zones, and the Earth from west to east after the rotation, followed by zero zone East zone to the East Area 31, then east 12 districts. 12 districts followed by the West, East 10 a district a district to the west, and finally back to zero zone.

12 districts in which East and West District 22 is the same time zone , also known as something 12 districts .

Overall, the time zone is divided into 12 districts of East and West as well as initial zone 12 districts, 12 districts where the stuff is the same time zone, so the total 24time zones.

php-timezone-method-gmt-geo.jpg

Example:

Because Beijing is located in the East eight districts, multiple time zone than the zero 8time zones, which means Beijing Standard Time Biglin fast 8 hours.

So GMT 12:00 the whole time, early in the morning until 4 o'clock in Greenwich, estimates still sleeping! ( GMT+8Beijing time)

GMT with the Earth's rotation about, can not meet the needs of precise timing, and therefore no longer available as standard time , replaced by Coordinated Universal Time.

Coordinated Universal Time

Coordinated Universal Time (also known as the world's unified time or UTC or Coordinated Universal Time , English is the Coordinated Universal Time , abbreviated UTC ), Coordinated Universal Time is based on atomic time -second basis, as close as possible in time GMT of a time-metering system.

In does not require accurate to seconds in the case, GMTand UTCbasically the same, but UTCis based on a more accurate atomic time, so commonly used in the field of scientific computing, it is currently the unified standard time measurement.

Example:

Beijing 12:00, converted GMTor UTCtime is 04:00( UTC+8also GMT)

Beijing

Beijing (also known as the China Standard Time ), the time zone is where the capital city of Beijing as China's standard time, than the GMT almost 8 hours.

China's vast territory, from west to east across the five East, East Sixth, East Seven, eight East and East and nine five time zones. Therefore, the use of a unified national capital, Beijing, where Timezone 8 time zone as the standard time, which is Beijing time.

Tz database

Tz database, also known as the Olson database, is mainly used in a computer program and the operating system can be time zone information for collaborative editing world database.

Tz database by using the "Area / Location" naming convention, easily applied to the computer world, the English names spaces with an underscore "_" instead, hyphen "-" only when the name itself comprises English.

Example:

  • Asia/Hong_Kong : Asia / Hong Kong
  • Asia/Macau : Asia / Macau
  • Asia/Shanghai : Asia / Shanghai
  • Asia/Taipei : Asia / Taipei
  • Asia/Urumqi : Asia / Urumqi

When the above-mentioned area is mainly phpsupported in China time zone, refer to the list of supported Asian time zone

Programming on the time zone of realization

Time zone is not only real-life problems, computer programming world also have the time, naturally, is inseparable from the concept of time zones.

In the computer world, everything is data, the concept of time zones can be best reflected in the relevant database, fortunately predecessors have provided us with tz database , which will provide the basic tools of various types of language class time zone.

phpThe date and time the library is php built-in function library , we can easily manipulate time, and so set the time zone.

Under normal circumstances, the dateextension is enabled by default, we can enter the phpinfoprint out phpbasic information, then search for the keyword dateyou can find dateinformation about extensions.

  • Examples
<?php
// 获取默认时区
echo "默认时区: ".ini_get("date.timezone")."<br/>";

// 获取当前时区
echo "当前时区: ".date_default_timezone_get()."<br/>";

// 当前时间
echo "当前时间: ".date("Y-m-d H:i:s")."<br>";

// 打印 php 信息
phpinfo();
?>
  • result

The default time zone:
the current time zone: UTC
current time: 2019-05-30 05:30:00

Options value
date / time support (Support for date and time) enabled (enabled)
timelib version (time library version) 2016.05(2016.05)
"Olson" Timezone Database Version ( Olsontime zone database version) 2018.5(2018.5)
Timezone Database (time zone database) internal (inside)
Default timezone (the default time zone) UTC (Coordinated Universal Time)

Thus, assuming that if the time zone is not set, the default time zone is Coordinated Universal Time , the time zone and Beijing compared to the slow 8hours!

Set time zone three positions

Read the phpdeveloper documentation can be found in the current time zone all the support list, sorted out the following major time zones on China.

  • Asia/Hong_Kong : Asia / Hong Kong
  • Asia/Macau : Asia / Macau
  • Asia/Shanghai : Asia / Shanghai
  • Asia/Taipei : Asia / Taipei
  • Asia/Urumqi : Asia / Urumqi

Common time zone is Shanghai, and Beijing does not, of course, can also be set PRC(People's Republic of China)!

If you do not set the time zone, then the default time zone should be Coordinated Universal Time ( UTC), although it is a common time standard, but still need some converted to local time conversion, but looks uncomfortable.

So better to modify the time zone settings, if the business does not consider international demand, then the time zone permanently fixed on it, if there is an international business scenario, it is best able to dynamically set the time zone, so that we can clearly know the local time.

Thus, the following mainly provides two ways to set the time zone, namely, static and dynamic settings provided, wherein the dynamic set and provides two methods.

Static modify phpconfiguration

phpThe default configuration file located in: /private/etc/php.iniafter opening the file modification date.timezoneoptions.

Since this method is to directly modify the configuration file, so the time zone setting applies to all scripts, just need to restart the server to take effect.

  • Examples
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =

; http://php.net/date.default-latitude
;date.default_latitude = 31.7667

; http://php.net/date.default-longitude
;date.default_longitude = 35.2333

; http://php.net/date.sunrise-zenith
;date.sunrise_zenith = 90.583333

; http://php.net/date.sunset-zenith
;date.sunset_zenith = 90.583333
  • result
# 去掉;并设置时区,取值可以是PRC,也可以是Asia/Shanghai等时区
date.timezone = PRC

For details, please refer to: http://php.net/date.timezone

Dynamic settings phpconfiguration

ini_set support setting the default zone options for the current script.

Static zone provided for fixing the case where only the time zone, if desired dynamic switching time zones, modify the configuration file can not meet such a situation.

Therefore, phpalso it provides a dynamic modification php.inimethod profile, ini_set()the method just to support dynamic time zone setting.

Without restarting the server, but only for the current script into force, which ini_setmethod supports the List of php.ini directives

  • Examples
// 设置当前时区
ini_set("date.timezone", "Asia/Tokyo");

// 获取默认时区
echo "当前时区: ".ini_get("date.timezone")."<br/>";

// 当前时间
echo "当前时间: ".date("Y-m-d H:i:s")."<br>";
  • result

The current time zone: Asia / Tokyo
Current time: 2019-05-30 14:30:00

Dynamic setting phptime zone

date_default_timezone_set method used to set the current script is the default time zone.

ini_setAlthough time zone support setting options, but after all, not a professional method when setting area, date_default_timezone_setis dedicated to the time zone settings.

Both methods belong to the dynamic time zone settings, are also take effect for the current script, also do not have to restart the server can immediately take effect.

  • Examples
// 获取当前时区
echo "当前时区: ".date_default_timezone_get()."<br/>";

// 设置当前时区
date_default_timezone_set("UTC");

// 获取当前时区
echo "当前时区: ".date_default_timezone_get()."<br/>";

// 当前时间
echo "当前时间: ".date("Y-m-d H:i:s")."<br>";
  • result

The current time zone: Asia / Tokyo
current time zone: UTC
current time: 2019-05-30 05:30:00

Some conclusions about the time zone

Time zone and time are closely related, unified time to say actually unified standard time, local time such a region can be converted easily to another region of the local time.

At present a unified world standard time is Coordinated Time ( UTC), China's standard time is GMT , GMT faster than Coordinated Universal Time 8hours.

Real world time zone should also be reflected in the computer world, where the tz database is used to represent the concept of real-world time zone.

Thus, the time zone is a universal concept, not only phptime zones, javaand jsother languages are also sometimes the concept of area, it can be said as long as there is time to place can not be separated zone.

Small Skills time zone, you getarrived?

  • American TV <<Flash>> eight pm local time on a Tuesday launch, when I ask Beijing launched?
  • British drama <<Sherlock>> GMT half past four launch, will begin broadcasting local time is when?
  • Thai drama <<new hard-nosed>> is twenty past nine p.m. local time launch, Beijing time is when I ask?

Reference material

Guess you like

Origin www.cnblogs.com/snowdreams1006/p/10949856.html