Php System time zone setting method summary

Source: Internet
Author: User
Tags current time echo date epoch time ini time zones local time time and date unix epoch time

Time zone Division

The whole earth is divided into 24 time zones, each time series has its own local time. At the same time, the local time in each time zone varies by 1 to 23 hours. For example, the local time in London is 8 hours different from that in Beijing. In the international radio communication field, a unified Time is used, called Universal Coordinated Time (UTC, Universal Time Coordinated). UTC is the same as Greenwich Mean Time (GMT, Greenwich Mean Time, both are the same as local time in London, England.

Time zone settings

Because PHP5.0 overwrites the data () function, the current date and time function is eight hours less than the system time. In PHP, the standard Greenwich Mean Time (zero time zone) is set by default. To obtain the current local time, you must change the time zone settings in PHP.

There are two ways to change the time zone settings in PHP:

(1) modify php. in the INI file, locate "; date." in [date. timezone = "option, change it to" date. timezone = Asia/Hong_Kong ", and then restart the Apache server.


Modify the PHP. ini file

Find the date. timezone line, remove the semicolon, and change it:

The code is as follows: Copy code

Date. timezone = Asia/Shanghai

Modify the. htaccess file

There are two ways to modify the. htaccess file: the following two statements only need one.

The code is as follows: Copy code

Php_value date. timezone Asia/Shanghai
SetEnv TZ Asia/Shanghai


(2) in the application, add the following functions before using the time and date functions:

Set the time zone in the code

The code is as follows: Copy code

1 date_default_timezone_set ('Asia/Shanghai'); // 'Asia/Shanghai' Asia/Shanghai

2 date_default_timezone_set ('Asia/Chongqing '); // The value of Asia/Chongqing is "Asia/Chongqing"

3 date_default_timezone_set ('prc'); // PRC is "People's Republic of China"

4i ni_set ('date. timezone ', 'etc/GMT-8 ');

5 ini_set ('date. timezone ', 'prc ');

6 ini_set ('date. timezone ', 'Asia/Shanghai ');

7 ini_set ('date. timezone ', 'Asia/Chongqing ');

Date_default_timezone_set ('prc ');

// PRC is the time zone of the People's Republic of China

Or

The code is as follows: Copy code

<? Php
Date_default_timezone_set ("Asia/Kolkata ");
Echo date ('D-m-y h: I: s'); // Returns IST
?>

The parameter is a recognizable time zone name of PHP. If the time zone name cannot be recognized by PHP, the system uses the UTC time zone. The PHP Manual provides a list of names of various time zones, including PRC (People's Republic of China) and Asia/Chongqing (Chongqing), Asia/Shanghai (Shanghai) or Asia/Urumqi (Urumqi), the names of these time zones are equivalent.


After the settings are complete, the date () function can be used normally without any time difference.

Current time of the output system

The above is a method to set the current system Time. Here, we will use another method to set the current system time, that is, directly before the date () function through date_default_timezone_set () the function sets the system time zone and outputs the current date and time of the system. The code is as follows:

The code is as follows: Copy code

<? Php
Echo "UTC time:". date ("Y-m-d H: I: s"); // display the default UTC time
Date_default_timezone_set ("PRC"); // use the time zone of the People's Republic of China
Echo "<br> ";
Echo "Beijing Time:". date ("Y-m-d H: I: s"); // output Beijing time
Echo "<br> ";
Echo "Chinese time:". date ("Y, m, d, H, I minute, s seconds"); // output the Chinese format Time
Echo "<br> ";
Echo "current time zone:". date_default_timezone_get (); // Obtain the current time zone
?>

Test:

The code is as follows: Copy code

Code
Date_default_timezone_set ('etc/GMT ');
// Set the time zone before the output Time. Etc/GMT is the standard time.
Echo (time ());
Echo ('
');
Echo date ('Y-m-d H: I: S', time ());
Echo ('
');
Echo gmdate ("Y-m-d H: I: s", time ());
Echo ('
');
Date_default_timezone_set ('prc ');
// Set the time zone before the output time. The PRC is the People's Republic of China.
Echo (time ());
Echo ('
');
Echo date ('Y-m-d H: I: S', time ());
Echo ('
');
Echo gmdate ("Y-m-d H: I: s", time ());
?>
Result:
1276257131
2010-06-11 11:52:11
2010-06-11 11:52:11
1276257131
2010-06-11 19:52:11
2010-06-11 11:52:11
Conclusion:
Time (): standard timestamp
Returns the number of seconds from the Unix epoch (January 1, 1970 00:00:00 GMT) to the current time (the current GMT Standard Time). Its value is irrelevant to the time zone set by the php system.
Date (): return the "custom format" time of the localization time of the current GMT Standard Time, which is related to the time zone set by the php system.
Gmdate (): returns the "custom format" time of the current GMT standard time, regardless of the time zone set by the php system.

Unix timestamp: the number of seconds between the current (GMT standard) time and the unix epoch time (00:00:00;
Code
Date_default_timezone_set ('etc/GMT ');
// Set the time zone before the output Time. Etc/GMT is the standard time.
Echo (strtotime ('2017-01-01 08:00:00 & prime ;));
Echo ('
');
Date_default_timezone_set ('prc ');
// Set the time zone before the output time. The PRC is the People's Republic of China.
Echo (strtotime ('2017-01-01 08:00:00 & prime ;));
?>
Test results:
28800
0

Strtotime (a): unix timestamp of the GMT standard time of the current time. If the current time zone set by the php system is not the GMT standard time zone, when strtotime (a) is applied, the system automatically converts time a to the corresponding GMT standard time, and then calculates the unix timestamp of the time. Related to the time zone set by the php System

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.