PHP date()Function


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


PHP date() Function

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

TipA timestamp is a sequence of characters representing the date/time at which a certain event occurred.

Syntax

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

Parameter Description
format Required. Specifies the format of the timestamp.
timestamp Optional. Specifies the timestamp. Default is the current date and time.


PHP Date() - Formatting Dates

The first required parameter of the date() functionformatspecifies how to format the date/time.

Here are some available characters:

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

To learn aboutformatthe list of all characters available in the parameter, please consult our PHP Date Reference Manual,date() function。

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

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

The output of the code above is shown below:

2016/10/21
2016.10.21
2016-10-21
The format string can recognize the followingformatstrings of the parameter
formatCharacter Description Return value examples
day --- ---
d Day of the month, 2 digits with leading zeros 01to31
D Day of the week, textual representation, 3 letters MontoSun
j Day of the month, without leading zeros 1to31
l(lowercase letter "L") Day of the week, full textual format SundaytoSaturday
N ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1(for Monday) to7(for Sunday)
S English ordinal suffix for the day of the month, 2 characters st,nd,rdorth. Can be used withjtogether
w Day of the week, numeric representation 0(for Sunday) to6(for Saturday)
z Day of the year 0to365
Week --- ---
W ISO-8601 week number of the year, weeks starting on Monday (added in PHP 4.1.0) Example:42(the 42nd week of the year)
month --- ---
F Month, full textual format, e.g. January or March JanuarytoDecember
m Numeric representation of the month, with leading zeros 01to12
M Three-letter abbreviation of the month JantoDec
n Numeric representation of the month, without leading zeros 1to12
t Number of days in the given month 28to31
year --- ---
L Whether it is a leap year 1 if it is a leap year,1otherwise 00
o ISO-8601 year number. This is the same asYsame as the value of, except if the ISOW) belongs to the previous or next year, then that year is used. (Added in PHP 5.1.0) Examples: 1999 or 2003
Y Year, 4 digits, fully represented Example:1999or2003
y Year, 2 digits Example:99or03
Time --- ---
a Lowercase Ante meridiem and Post meridiem values amorpm
A Uppercase Ante meridiem and Post meridiem values AMorPM
B Swatch Internet Time 000to999
g Hour, 12-hour format, without leading zeros 1to12
G Hour, 24-hour format, without leading zeros 0to23
h Hour, 12-hour format, with leading zeros 01to12
H Hour, 24-hour format, with leading zeros 00to23
i Minutes with leading zeros 00to59>
s Seconds with leading zeros 00to59>
u Milliseconds (added in PHP 5.2.2). Note thatdate()the function always returns000000because it only acceptsintegerparameter, andDateTime::format()supports milliseconds. Example:654321
Time zones --- ---
e Timezone identifier (added in PHP 5.1.0) Example:UTC,GMT,Atlantic/Azores
I Whether it is daylight saving time 1 if it is daylight saving time,1otherwise 00
O Difference to Greenwich time (GMT) in hours Example:+0200
P Difference to Greenwich time (GMT), with colon between hours and minutes (added in PHP 5.1.3) Example:+02:00
T Timezone of the local machine Example:EST,MDT(Translator's note: Under Windows it is in full textual format, e.g. "Eastern Standard Time"; the Chinese version will display "China Standard Time").
Z The timezone offset in seconds. The offset west of UTC is always negative, and the offset east of UTC is always positive. -43200to43200
Full date/time --- ---
c ISO 8601 formatted date (added in PHP 5) 2004-02-12T15:19:21+00:00
r RFC 822 formatted date Example:Thu, 21 Dec 2000 16:01:07 +0200
U The number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) to the present See alsotime()

Complete PHP Date Reference Manual

For a complete reference manual of all date functions, please visit ourComplete PHP Date Reference Manual。

This reference manual provides a brief description and application examples for each function!

Other Extensions