The PHP date () function obtains 8 hours less than the current time.

Source: Internet
Author: User
Tags echo date iso 8601 iso 8601 format timezones what php

If you want to get the current time for PHP beginners, you can use the time function date () to format a local time/date and write a test code:

<〈? PHP
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 ');

The date. timezone option is added to PhP. ini at php5.1. it 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 time.

1. The simplest method is not to use php5.1 or a later version-obviously this is not an advisable method !!!

2. 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.

3. Add the initialization Statement of time to the program: "date_default_timezone_set (" Asia/Shanghai ");" which can be set by the programmer at will.
Recommendation.
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 regions: Asia/Macao, Asia/hong_kong, Asia/Taipei (in the order of Macao, Hong Kong, and Taipei)
Singapore: Asia/Singapore

In this way, the output is Beijing time. The more detailed time zone code, the time zone code, and the time zone index code.Timezone_identifierYou can check it on the official website.

Http://cn2.php.net/manual/en/function.date-default-timezone-set.php to view the list of parameters
Of supported timezones.

Or directly view: http://cn2.php.net/manual/en/timezones.php

 

Appendix:
Date
(PHP 3, PHP 4, PHP 5)

Date -- format a local time/date
Description
String date (string format [, int timestamp])

Returns the string generated by the integer timestamp according to the given format string. If no timestamp is provided, the current local time is used. In other words, timestamp is optional and the default value is time ().
Table 1. The format strings can recognize the followingFormatParameter string

FormatCharacter Description Return Value example
Day --- ---
D The day of the month, which has two digits leading to zero 01To31
D The day of the week. The text indicates three letters. MonToSun
J The day of the month, with no leading zero 1To31
L(Lowercase letters of "L) Day of week, complete text format SundayToSaturday
N The day of the week represented by a number in the ISO-8601 format (New in PHP 5.1.0) 1(Monday)7(Sunday)
S English suffix after the number of days per month, 2 Characters St,Nd,RdOrTh. And
JUse together
W The day of the week, represented by a number 0(Sunday)6(Saturday)
Z The day of the year. 0To366
Week --- ---
W The week of the year in ISO-8601 format, starting from Monday each week (New in PHP 4.1.0) For example:42(The first week of the year)
Month --- ---
F The month in the complete text format, such as January or march. JanuaryToDecember
M Number indicates the month, with a leading zero 01To12
M The month abbreviated to three characters. JanToDec
N Number indicates the month, with no leading zero 1To12
T Number of days in a given month 28To31
Year --- ---
L Is it a leap year? If the leap year is1Otherwise0
O The number of years in ISO-8601 format. This andYExcept for the week number of ISO (W) Belongs to the previous year or the next year, that year is used. (New PHP 5.1.0) Examples:1999Or2003
Y The year in which the four digits represent the complete number. For example:1999Or2003
Y A two-digit year For example:99Or03
Time --- ---
A Morning and afternoon values in lower case AMOrPM
A Upper-case morning and afternoon values AMOrPM
B Swatch Internet standard 000To999
G Hour, 12-hour format, no leading zero 1To12
G Hour, in 24-hour format, no leading zero 0To23
H Hour, 12-hour format, with leading zero 01To12
H Hour, in 24-hour format, with a leading zero 00To23
I Minutes with a leading zero 00To59>
S Number of seconds, with a leading zero 00To59>
Time Zone --- ---
E Time zone ID (New in PHP 5.1.0) For example:UTC,GMT,Atlanta/Azores
I When it is enabled or not If it is enabled1Otherwise0
O Hours different from Greenwich Mean Time For example:+ 0200
T Time zone of the Local Machine For example:EST,MDT([Note] the full text format is in windows. For example, "Eastern Standard Time", the Chinese version displays "China Standard Time ").
Z The number of seconds of the time difference offset. The Time Zone offset to the west of UTC is always negative, and the time zone offset to the east of UTC is always positive. -43200To43200
Complete date/time --- ---
C Date in ISO 8601 format (New in PHP 5) 2004-02-12t15: 19: 21 + 00: 00
R Date in RFC 822 format For example:Thu, 21 Dec 2000 16:01:07 + 0200
U Number of seconds since UNIX epoch (January 1 1970 00:00:00 GMT) SeeTime ()

Author: Lu Haipeng
From: itstudy.cn original
Update log: the revised version of v0.2 has been completed;
Reference:
Thanks:
Discussion URL: http://www.deepteach.com/bbs/
Tag (TAGS): PhP date

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.