How to solve the time zone problem encountered in PHP, php time zone _ PHP Tutorial

Source: Internet
Author: User
Tags what php
How to solve the time zone problem encountered in PHP: php time zone. The solution to the time zone problem encountered in PHP is that the formatting timestamp in php is eight hours later than the Beijing time, how to solve the time zone problem that is not encountered in PHP during online search? php time zone

Recently, I found that the formatting timestamp in PHP is 8 hours later than that in Beijing time. I found that the time zone was incorrect during online search. the solution is as follows:

1. permanent modification

Change data. timezone = PRC in the php. ini file. this is China time. It takes effect after you restart the Apache service.

2. temporary modification

Before formatting the input time, add ini_set ('date. timezone ', 'Asia/Shanghai ');
Or data_default_timezone_set ('prc'); // date_default_timezone_set () function is used to set the default time zone of all date/time functions in the script.

Look at the problems encountered by other friends.

There was a legacy problem, that is, echo date ("Y-m-d H: I: s", time () is always not the same as the actual time, I finally found the cause and solution on the Internet today, and shared the following:

In addition, follow the instructions below to modify php. ini cannot find date. in timezone, is there no way to do this? of course not. if haha doesn't do it, add it by yourself. With such a statement date. timezone = "PRC", the problem is fixed.

From php5.1.0, the date. timezone option is added to php. ini, which is disabled by default.

That is, the displayed time (no matter what php command is used) is Greenwich Mean Time, which is exactly eight hours different from our time (Beijing time, there are three methods to restore the normal time.

1. the simplest method is not to use php5.1 or a later version.

2. if it must be used and php. ini cannot be modified, add date_default_timezone_set (XXX) to the time initialization statement );

3. once and for all, only php. ini can be modified. Open php. ini and find date. timezone. remove the semicolon.
= Add XXX to the backend and restart the http service (such as apache2 or iis ).

For XXX, the available values in mainland China are: Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (Chongqing, Shanghai, and Urumqi in sequence)

Available in Hong Kong and Taiwan regions: Asia/Macao, Asia/Hong_Kong, Asia/Taipei (in the order of Macao, Hong Kong, and Taipei)

Singapore: Asia/Singapore

Foreigners seem to have missed Beijing

Other available values are: Etc/GMT-8, Singapore, Hongkong, PRC

What is PRC? PRC is the People's Republic of China -_-

---------------------------------------

Solution: Use date_default_timezone_set () in the header to set my default time zone to Beijing time date_default_timezone_set ('prc ');
Echo date ('Y-m-d H: I: s'); time is the same as the current server time !! Congratulations! the usage of date_default_timezone_set is as follows:

-----------

Date_default_timezone_set
(PHP 5> = 5.1.0RC1)
Date_default_timezone_set-set the default time zone for all date and time functions in a script.
Bool date_default_timezone_set (string timezone_identifier)
Date_default_timezone_set () sets the default time zone for all datetime functions.
Note: Since PHP 5.1.0 (this version of the DATETIME function has been rewritten), if the time zone does not comply with the rules, every call to the DATETIME function will generate an E_NOTICE-level error message.

The above is all the content of this article. I hope you will like it.

Parse recently found that the formatting timestamp in PHP is eight hours later than that in Beijing time. it was found that the time zone was not the same during online search...

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.