Using the DATE function in PHP to get the current time error resolution _php tips

Source: Internet
Author: User
Tags add time echo date local time what php

Beginner PHP to do the website, want to get the current time on the page, the person who has studied programming knows to use the time function date (), first uses this function to format a local time/date, first writes a test code, the result output time is less than the actual time 8 hours, this is what reason:

Copy Code code as follows:

<?php
echo Date (' y-m-d h:i:s ');
?〉

Output Current time: 2008-10-12 02:32:17
Strange, the actual time is: 2008-10-12 10:32:17

is PHP's date () less than 8 hours in the correct time?
Take a look at the PHP manual, "Example 1." Date () example "One More time zone setting in the first line"
Set the default time zone to use. Available from PHP 5.1
Date_default_timezone_set (' UTC ');

Originally php5.1, the php.ini added date.timezone this option, by default is closed, that is, the display time (no matter what PHP command) is Greenwich Standard Time, and Beijing time is just 8 hours.

How can I set the right PHP time?

1, modify php.ini. Open php.ini Find date.timezone Remove the preceding semicolon = Add Asia/shanghai back, restart the Apache server-The disadvantage is that if the program
Put on someone else's server, can not modify the php.ini, it is not.

2, in the program to add time to the initialization of the statement is: "Date_default_timezone_set (" Asia/shanghai "); "This can be arbitrarily set by the programmer, my recommendation."
Time zone identifier, the available values in mainland China are: prc,asia/chongqing, Asia/shanghai, Asia/urumqi (China, Chongqing, Shanghai, Urumqi), Etc/gmt-8,asia/harbin

Taiwan available: Asia/macao, Asia/hong_kong, Asia/taipei (Macao, Hong Kong, Taipei) and Singapore: Asia/singapore
This can be achieved in the output of Beijing time.

More detailed time zone code time zone coded time Zone index code timezone_identifier you can find it in 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.