Home > php教程 > php手册 > body text

Detailed explanation of the use of date and time function date() in PHP function_php basics

WBOY
Release: 2016-05-16 09:00:04
Original
1946 people have browsed it

Date and time functions are a core component of PHP. No installation is required to use these functions. Let’s talk about the specific usage of date function in detail:

PHP Date() Function
The PHP Date() function formats a timestamp into a more readable date and time.

Syntax
date(format,timestamp)

format required. Specifies the format of the timestamp.
timestamp optional. Specify timestamp. The default is the current date and time.


PHP’s date and time function date()

Copy code The code is as follows:

$t=time();
echo date("Y-m-d H:i:s",$t);

The format of the first parameter respectively represents:
a - "am" or "pm"
A - "AM" or "PM"
d - day, two digits, If there are less than two digits, add zeros in front; such as: "01" to "31"
D - day of the week, three English letters; such as: "Fri"
F - month, full English name; such as: " January"
h - hour in 12-hour format; such as: "01" to "12"
H - hour in 24-hour format; such as: "00" to "23"
g - 12-hour format For hours in 24-hour format, do not add zeros for less than two digits; for example: "1" to 12"
G - For hours in 24-hour format, do not add zeros for less than two digits; for example: "0" to "23"
i - Minutes; such as: "00" to "59"
j - day, two digits, if there are less than two digits, do not add zero; such as: "1" to "31"
l - day of the week, in full English name; such as: "Friday"
m - month, two digits, if less than two digits, add zeros in front; such as: "01" to "12"
n - month, two digits, if less than two digits If two digits are used, zeros will not be added; such as: "1" to "12"
M - month, three English letters; such as: "Jan"
s - seconds; such as: "00" to "59"S - add an English ordinal number at the end of the word, two English letters; such as: "th", "nd"
t - specify the number of days in the month; such as: "28" to "31"
U - total seconds Number
w - numeric day of the week, such as: "0" (Sunday) to "6" (Saturday)
Y - year, four digits; such as: "1999"
y - year, Two digits; such as: "99"
z - the day of the year; such as: "0" to "365"
Other characters not listed above will be listed directly

1, year-month-day

Copy code The code is as follows:

echo date('Y-m-j');2007-02-6

echo date('y-n-j');
07-2-6


Capital Y means four digits of the year numbers, while lowercase y represents the two-digit number of the year;
lowercase m represents the number of the month (with a leading), while lowercase n represents the number of the month without the leading.

Copy code The code is as follows:

echo date('Y-M-j' );
2007-Feb-6

echo date('Y-m-d');
2007-02-06

The uppercase M represents the 3 abbreviation characters of the month, while the lowercase m represents the number of the month (with leading 0);
There is no uppercase J, only the lowercase j represents the date of the month, without the leading o; if the month is required Use a lowercase d when leading.

Copy code The code is as follows:

echo date('Y-M-j' );
2007-Feb-6

echo date('Y-F-jS');
2007-February-6th


Capital M represents the three abbreviated characters of the month, while capital F represents the full English character of the month. (No lowercase f)
Capital S represents the suffix of the date, such as "st", "nd", "rd" and "th", depending on the date number.

Summary:
The year can be represented by uppercase Y and lowercase y;
The month can be represented by uppercase F, uppercase M, lowercase m and lowercase n (two ways to represent characters and numbers respectively) );
You can use lowercase d and lowercase j to represent the day, and uppercase S represents the suffix of the date.


2, hours: minutes: seconds

By default, the time displayed by PHP interpretation is "Greenwich Mean Time", which is 8 hours different from our local time.

Copy code The code is as follows:

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

echo date('h:i:s A');
05:56:57 AM

A lowercase g indicates a 12-hour format without leading 0s, while a lowercase h indicates a 12-hour format with leading 0s.
When using the 12-hour clock, it is necessary to indicate morning and afternoon. Lowercase a represents lowercase "am" and "pm", and uppercase A represents uppercase "AM" and "PM".

Copy code The code is as follows:

echo date('G:i:s') ;
14:02:26

Capital G represents the hour in the 24-hour system, but without a leader; use capital H to represent the hour in the 24-hour system with a leader

Summary:
The letter g represents the hour without a leader , the letter h represents the hour with a leading;
lowercase g and h represent the 12-hour format, and capital G and H represent the 24-hour format.

3, leap year, week, day

Copy code The code is as follows:

echo date('L');
Whether this year is a leap year: 0

echo date('l');
Today is: Tuesday

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

Capital L means to determine whether this year is a leap year, Boolean value, returns 1 if true, otherwise 0;
Small l represents the full English version of the day of the week (Tuesday);
Use capital D to represent the day of the week The 3-character abbreviation of a few (Tue).

Copy code The code is as follows:

echo date('w');

Today’s week: 2

Copy code The code is as follows:

echo date('W');

This week is the 06th week of the year

The lowercase w represents the day of the week, expressed in numeric form
The uppercase W represents the number of weeks in the year

Copy code The code is as follows:

echo date('t');

This month is 28 days

Copy code The code is as follows:

echo date('z');

Today is the 36th day of this year

Lowercase t represents the number of days in the current month
Lowercase z represents the day of the year today

4, others

Copy code The code is as follows:

echo date('T');
UTC

Capital T indicates the server’s time zone setting

Copy code The code is as follows:

echo date('I');
0

Capital I means to determine whether the current daylight saving time is, if true, return 1, otherwise 0

Copy code The code is as follows:

echo date('U');
1170769424

The uppercase U represents the total number of seconds from January 1, 1970 to the present, which is the UNIX timestamp of the Unix time epoch.

Copy code 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. The letter T is used to separate the date and time. The time format is HH:MM:SS. The time zone is represented by the offset from Greenwich Mean Time (GMT). .

Copy code The code is as follows:

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

Lowercase r represents the RFC822 date.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template