PHP date () function output Time example

Source: Internet
Author: User
Tags current time echo date local time lowercase

PHP can display the date and time of the server through the date () function, and here's a detailed look at how to use the date () function to output the date:

php Date () function detailed

1, year-month-day

The code is as follows Copy Code

echo Date (' y-m-j ');

echo Date (' y-n-j ');

Capital Y represents a four-digit year, while lowercase y represents a two-digit number for the year;
Lowercase m represents the number of months (with leading), while lowercase n indicates the number of months without leading.

The code is as follows Copy Code

echo Date (' y-m-j ');

echo Date (' y-m-d ');

Uppercase M represents the 3 abbreviations for the month, while lowercase m represents the number of months (with a leading 0);
There is no uppercase J, only lowercase J indicates the date of the month, no leading o, and lowercase D if you want the month band leading.

The code is as follows Copy Code

echo Date (' y-m-j ');

echo Date (' Y-f-js ');

Capital M represents the 3 abbreviations of the month, while capital F indicates the full English of the month. (No lowercase f)
Uppercase S represents the suffix of a date, such as "St", "nd", "rd", and "th", depending on the date number.

Indicates that the year can be in uppercase Y and lowercase y;
Indicates that the month can be in uppercase F, uppercase M, lowercase m, and lowercase n (two ways of representing characters and numbers, respectively);
Indicates that the day can be a suffix of a date with lowercase d and lowercase j, uppercase S.

2, when: minutes: seconds

By default, PHP interprets the time as "Greenwich Mean Time", which is 8 hours away from our local time.

The code is as follows Copy Code

echo Date (' g:i:s a ');
5:56:57 am

echo Date (' H:i:s A ');
05:56:57 AM

Lowercase g represents a 12-hour system with no leading 0, while lowercase h indicates a 12-hour system with a leading 0.
When using a 12-hour system, you need to indicate that in the afternoon, lowercase a is the lowercase "am" and "PM", and capital a denotes "AM" and "PM" uppercase.

The code is as follows Copy Code

echo Date (' g:i:s ');

Capital G represents 24 hours of hours, but not leading; use uppercase H to indicate a 24 hour system with a leading number of hours

The letter G indicates that the hour is not leading, and the letter H denotes the hour with leading;
Lowercase g, h means 12-hour system, capital G, H is 24-hour system.

3, Leap year, week, day

The code is as follows Copy Code

echo Date (' L ');
Whether this year leap years: 0

echo Date (' L ');
Today is: Tuesday

echo Date (' D ');
Today is: Tue

Capital L indicates whether a leap year is a Boolean value that returns 1 for true or 0;
Lowercase L means the day is the week of the English full write (Tuesday);
Instead, use uppercase D to denote the 3-character abbreviation for the Day of the Week (Tue).

The code is as follows Copy Code

echo Date (' W ');
Today's Week: 2

echo Date (' W ');
This week is the No. 06 week of the year.

The lowercase w represents the day of the week, the number form represents
Capital W indicates the number of weeks in a year

echo Date (' t ');
This month is 28 days

echo Date (' Z ');
Today is the 36th day of the year.

Lowercase T represents the current month and how many days
Lowercase Z means today is the first day of the year

4, other

The code is as follows Copy Code

echo Date (' T ');
Capital T indicates the time zone setting of the server

echo Date (' I ');
Capital I indicates whether the current is daylight saving time, returns 1 for true, or 0

echo Date (' U ');
The capital U represents the total number of seconds from January 1, 1970 to the present, which is the Unix time stamp for the Unix time era.

echo Date (' C ');
Lowercase C represents the ISO8601 date, the date format is YYYY-MM-DD, the date and time is separated by the letter T, the time format is HH:MM:SS, and the time zone uses the deviation of Greenwich Mean Time (GMT).

echo Date (' R ');
Tue, Feb 2007 14:25:52 +0000
Lowercase R represents the RFC822 date.

Time zone problem in date function

Date ("y-m-d h:i:s") Totals server time by a few hours

The workaround is as follows:

First, use Date_default_timezone_set () in the header to set my default time zone for the year

The code is as follows Copy Code
Date_default_timezone_set (' PRC ');
echo Date (' y-m-d h:i:s ');

Time is the same as the server current time!!

Second, modify the php.ini.

Open Php5.ini Find Date.timezone Remove the preceding semicolon = Add xxx back, restart the HTTP service

(such as apache2 or IIS, etc.).
For XXX, the values available in mainland China are: asia/chongqing, Asia/shanghai,

Asia/urumqi (Chongqing, Shanghai, Urumqi in turn) Taiwan available: Asia/macao,

Asia/hong_kong, Asia/taipei (Macao, Hong Kong, Taipei) and Singapore:

Asia/singapore foreigners seem to have leaked Beijing. Other available values are: Etc/gmt-8, Singapore,

Hongkong, what's PRC,PRC? PRC is the People's Republic of China Ah-_-above all from the PHP official documentation

The areas that are sorted out under the gmt+8 may be omitted.

Date function month and day band 0 problem

The code is as follows Copy Code

One, with 0

echo Date (' y-m-d ');

Two, do not take 0

echo Date (' y-n-j ');

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