Php: incorrect current time obtained using the date function

Source: Internet
Author: User
Tags what php
PHP is a beginner who wants to obtain the current time on the page. anyone who has learned programming knows to use the time function date () to format a local time/date with this function, write a test code first. The result output time is eight hours earlier than the actual time. why? [php] & lt ;? Phpecho... "/> <scripttype =" text/javascript "src =" ht

PHP is a beginner who wants to obtain the current time on the page. anyone who has learned programming knows to use the time function date () to format a local time/date with this function, write a test code first. The result output time is eight hours earlier than the actual time. why:


[Php]
Echo date ('Y-m-d H: I: s ');
? > 〉

Echo date ('Y-m-d H: I: s ');
? > 〉
Output Current time: 02:32:17
The actual time is 10:32:17.

Is it because the date () time in PHP is incorrect and 8 hours less?
Let's take a look at the "Example 1. date () example" in the PHP Manual. The first line has a time zone setting.

// Set the default time zone to use. Available in PHP 5.1
Date_default_timezone_set ('utc ');

Php. date is added to ini. the timezone option 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 behind Beijing time.

How to set the correct PHP time.

1. modify php. ini. Open php. ini to search for date. timezone and remove the semicolon = followed by Asia/Shanghai. restart the apache server. The disadvantage is that if the program
Put it on someone else's server and cannot modify php. ini.

2. add the initialization statement of time to the program: "date_default_timezone_set (" Asia/Shanghai ");" which can be set by the programmer at will. I recommend this statement.
Time zone identifier, available value in mainland China: PRC, Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (China, Chongqing, Shanghai, Urumqi), Etc/GMT-8, asia/Harbin

Available in Hong Kong and Taiwan: Asia/Macao, Asia/Hong_Kong, Asia/Taipei (in the order of Macao, Hong Kong, and Taipei) and Singapore: Asia/Singapore
In this way, the output is Beijing time.
For more details about the time zone code, the time zone code, the time zone index code timezone_identifier, which can be found on the official website.

 


 

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.