Example of modifying the default time zone of php in PHP

Source: Internet
Author: User
Tags current time echo date ini time zones local time php script


Each region has its own local time. In online and radio communication, the time conversion problem is particularly prominent. The whole earth is divided into 24 time zones, each time zone has its own local time. For international radio or network communications, Universal Time Coordinated (UTC) is the global standard Time set by world standards. UTC was originally converted into Greenwich Mean Time (GMT), which is the same as the local Time in London.

The default time zone of PHP is UTC, while Beijing is located in the UTC + 8 zone, leading the UTC8 hour. Therefore, when using a function such as time () in PHP to obtain the current time, the obtained time is always incorrect, which is 8 hours different from the Beijing time. If you want to correctly display the Beijing time, you need to modify the default time zone settings. You can do so in either of the following ways.

① If you are using an independent server and have the permission to modify the configuration file, you can set the time zone by modifying the date. timezone attribute in php. ini. We can set the value of this property to one of "Asia/Shang", "Asia/Chongqing", "Etc/GMT-8", or PRC, the current time obtained in the PHP script is Beijing time. Modify the PHP configuration file as follows:
Date. timezone = Etc/GMT-8 // set the default time zone in the configuration file to East 8 (Beijing time)

② If you are using a shared server, you are not authorized to modify the configuration file php. ini, and the PHP version is later than 5.1.0, you can also call the date_dafault_timezone_set () function to set the time zone before the output time. This function requires a time zone identifier as a parameter, which is the same as the value of the date. timezone attribute in the configuration file. The function is used as follows:

Date_default_timezone_set ('prc'); // The time zone set before the output Time. PRC is the People's Republic of China.
Echo date ('Y-m-d H: I: S', time (); // The current output time is Beijing time.

3. Use php functions

If you use Beijing time, just like this. Date_default_timezone_set ("Asia/Shanghai ");

For example, the default time zone of WAMP

The default time zone of WAMP is Greenwich Mean Time (GMT), which means that the default server time zone of WAMP is GMT, but we can change the default time zone of the server to correspond to Beijing time. Open php. ini in the WAMP menu and add the following code at the end of the document:

[Date]
; Defines the default timezone used by the date functions
Date. timezone = "Asia/Shanghai"

Related Article

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.