PHP 5 Date and Time

The PHP date () function is used to format the time /date.

PHP date () function

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

Tip The timestamp is a sequence of characters representing the date /time when a certain event occurred.


string date (string $ format [, int $ timestamp])

parameters Description
format Required. Specifies the format of the timestamp.
timestamp Optional. Specifies the timestamp. The default is the current date and time.

PHP Date ()-formatted date


date () function's first required parameter format specifies how to format the date /time.

Here are some available characters:

  • d-day of the month (01-31)
  • m-represents the month (01-12)
  • Y-represents the year (four digits)

For a list of all characters available in the format parameter, check out our PHP Date reference manual, date () function .

You can insert other characters between the letters, such as "/", "." or "-", so you can add additional formatting:

echo date("Y/m/d") . "<br>";
echo date("Y.m.d") . "<br>";
echo date("Y-m-d");

The output of the above code is shown below:

The format string recognizes the following format parameter string
format character Description Example return value
Day --- ---
d Day of the month, 2 digits with leading zero 01 to 31
D Day of the week, text indicates 3 letters Mon to Sun
j Day of the month without leading zeros 1 to 31
l (lowercase "L") Day of the week, full text format Sunday to Saturday
N Day of the week as a number in ISO-8601 format (added in PHP 5.1.0) 1 (for Monday) to 7 (for Sunday)
S English suffix after the number of days per month, 2 characters st , nd , rd Or th . Can be used with j
w Day of the week, numbered 0 (for Sunday) to 6 (for Saturday)
z Day of the year 0 to 365
Week --- ---
W The week of the year in ISO-8601 format, each week starts on Monday (added in PHP 4.1.0) Example: 42 (week 42 of the year)
month --- ---
F Month, full text format, such as January or March January to December
m Month represented by numbers, with leading zeros 01 to 12
M Three-letter abbreviated month Jan to Dec
n Month represented by numbers without leading zeros 1 to 12
t Number of days in a given month 28 to 31
year --- ---
L Is it a leap year? If you are a leap year, 1 , otherwise 0
o ISO-8601 year number. with Y has the same value except that if the ISO Of the week ( W ) belongs to the previous or next year, the year is used. (New in PHP 5.1.0) Examples: 1999 or 2003
Y Your year represented by a 4-digit number Example: 1999 or 2003
y 2-digit year Example: 99 or 03
Time --- ---
a Morning and afternoon values ​​in lower case am or pm
A Capital morning and afternoon values ​​ AM or PM
B Swatch Internet Standard Time 000 to 999
g Hour, 12-hour format, without leading zeros 1 to 12
G Hour, 24-hour format without leading zeros 0 to 23
h Hour, 12-hour format, with leading zeros 01 to 12
H Hour, 24-hour format with leading zeros 00 to 23
i Minutes with leading zeros 00 to59>
s seconds with leading zeros 00 to59>
u Milliseconds (added in PHP 5.2.2). have to be aware of is The date () function always returns 000000 because it only accepts integer Parameter, DateTime :: format () supports milliseconds. Example: 654321
Time zone --- ---
e Time zone identifier (new in PHP 5.1.0) Example: UTC , GMT , Atlantic /Azores
I Daylight saving time 1 if it is daylight saving time, otherwise 0
O Hours from Greenwich time Example: +0200
P Difference from Greenwich Mean Time (GMT), with a colon between the hour and minute (added in PHP 5.1.3) Example: +02: 00
T time zone where the unit example: EST , MDT (in Windows] [Translator's Note Below is the full text format, such as "Eastern Standard Time", the Chinese version will display "China Standard Time").
Z The time in seconds of the time 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. -43200 to 43200
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 Example: Thu, 21 Dec 2000 16:01:07 +0200
U Seconds since the Unix Era (January 1 1970 00:00:00 GMT) See time ()