How to use the date () function of the PHP function _ PHP Tutorial

Source: Internet
Author: User
Use of the date () function of the PHP function. The DATETIME function is the core component of PHP. These functions can be used without installation. The following describes the specific usage of the date function in detail: the PHPDate () function of PHPDate () can regard the time and date functions as the core component of PHP. These functions can be used without installation. The following describes the specific usage of the date function:

PHP Date () function
The PHP Date () function can format the timestamp as a Date and time with better readability.

Syntax
Date (format, timestamp)

Format is required. The format of the specified timestamp.
Optional. The specified timestamp. The default value is the current date and time.


PHP date and time functions date ()

The code is as follows:


$ T = time ();
Echo date ("Y-m-d H: I: s", $ t );

The format of the first parameter is as follows:
A-"am" or "pm"
A-"AM" or "PM"
D-a few days, two digits. if there are less than two digits, fill in the first zero. for example, "01" to "31"
D-the day of the week, with three English letters, for example, "Fri"
F-month, full English name; for example: "January"
H-12 hours, for example, "01" to "12"
H-24 hours, for example, "00" to "23"
Hours in the g-12 hour format. less than two hours do not fill in zero. for example: "1" to 12"
Hours in the G-24-hour format. less than two hours do not fill in zero. for example: "0" to "23"
I-minutes; for example: "00" to "59"
J-a few days, two digits. if less than two digits are left blank, for example, "1" to "31"
L-the day of the week, full name in English; for example: "Friday"
M-month, two digits. if there are less than two digits, add zero in front, for example, "01" to "12"
N-month, two digits. if less than two digits are left blank, for example, "1" to "12"
M-month, with three English letters, for example, "Jan"
S-seconds; for example: "00" to "59"
The end of the S-character is followed by an English sequence, with two English letters, for example, "th", "nd"
T-specifies the number of days in a month, for example, "28" to "31"
U-total seconds
W-number of weeks, for example, "0" (Sunday) to "6" (Saturday)
Y-year, four digits, such as: "1999"
Y-year, two digits, for example, "99"
Z-the day of the year, for example, "0" to "365"
Other characters not in the upper column are listed directly.

1, year-month-day

The code is as follows:


Echo date ('Y-m-J ');
2007-02-6

Echo date ('Y-n-J ');
07-2-6



Uppercase Y indicates four digits of the year, while lowercase y indicates two digits of the year;
Lowercase m indicates the number of the month (with the leading digit), while lowercase n indicates the number of the month without the leading digit.

The code is as follows:


Echo date ('Y-M-J ');
2007-Feb-6

Echo date ('Y-m-D ');
2007-02-06

Uppercase M indicates the three abbreviations of the month, while lowercase m indicates the number of the month (with leading 0 );
There is no upper-case J. only lower-case j indicates the date of the month, and there is no leading o. if the month needs to have the upper-case J, the lower-case d is used.

The code is as follows:


Echo date ('Y-M-J ');
2007-Feb-6

Echo date ('Y-F-js ');
2007-February-6th


Uppercase M indicates the three abbreviated characters of the month, while uppercase F indicates the full English writing of the month. (No lower case f)
Uppercase S indicates the suffix of a date, such as "st", "nd", "rd", and "th". for details, see the date number.

Summary:
Y indicates that the year can be in uppercase or lowercase y;
Indicates that the month can be written in uppercase letters (F), uppercase letters (M), lowercase letters (m), and lowercase letters (n );
Indicates the date suffix.


2, hour: minute: Second

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

The code is as follows:


Echo date ('G: I: s ');
5:56:57 am

Echo date ('H: I: s ');
05:56:57 AM

Lowercase g indicates the 12-hour system, with no leading 0, while lowercase h indicates the 12-hour system with leading 0.
When the 12-hour format is used, it indicates that upper afternoon, lower case a indicates lower case "am" and "pm", and upper case A indicates upper case "AM" and "PM ".

The code is as follows:


Echo date ('G: I: s ');
14:02:26

Uppercase G indicates the number of hours in the 24-hour format, but does not contain the leading value. uppercase H indicates the number of hours in the 24-hour format.

Summary:
The letter g indicates that the hour does not contain a leading character, and the letter h indicates that the hour contains a leading character;
Lowercase g and h are in 12-hour format, while uppercase G and H are in 24-hour format.

3, leap year, week, day

The code is as follows:


Echo date ('L ');
This year's leap year: 0

Echo date ('L ');
Today is: Tuesday

Echo date ('D ');
Today is: Tue

Capital L indicates whether to determine the leap year of the year. if it is true, 1 is returned; otherwise, 0 is returned;
Lowercase l indicates the day of the week in full (Tuesday );
However, uppercase D is used to represent the three abbreviation of the day of the week (Tue ).

The code is as follows:


Echo date ('w ');

Today's week: 2

The code is as follows:


Echo date ('w ');

This week is the 06th week of the year.

Lowercase w indicates the day of the week, expressed in numbers
In upper case, W indicates the number of weeks in a year.

The code is as follows:


Echo date ('t ');

This month is 28 days

The code is as follows:


Echo date ('Z ');

Today is the 36th day of this year

Lowercase t indicates the number of days in the current month
Lowercase z indicates that today is the day of the year

4. others

The code is as follows:


Echo date ('t ');
UTC

Uppercase T indicates the time zone setting of the server

The code is as follows:


Echo date ('I ');
0

If I is used to determine whether the current value is success, 1 is returned for true. otherwise, 0 is returned.

The code is as follows:


Echo date ('u ');
1170769424

The upper-case u table shows the total number of seconds from January 1, January 1, 1970 to the present, which is the Unix timestamp of the UNIX time era.

The code is as follows:


Echo date ('C ');
2007-02-06T14: 24: 43 + 00: 00

Lowercase c represents the ISO8601 date, the date format is YYYY-MM-DD, with the letter T to interval the date and time, the time format is HH: MM: SS, the time zone uses Greenwich Mean Time (GMT).

The code is as follows:


Echo date ('r ');
Tue, 06 Feb 2007 14:25:52 + 0000

Lowercase r indicates the RFC822 date.

The core component of the pipeline. These functions can be used without installation. Next let's talk about the specific usage of the date function in detail: PHP Date () function can handle the hour...

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.