PHP date() function is used to format time/date.
The PHP date() function formats a timestamp into a more readable date and time.
A timestamp is a sequence of characters that represents the date/time when a certain event occurred.
string date ( string $format [, int $timestamp ] )
parameter | describe |
---|---|
format | Required. Specifies the format of the timestamp. |
timestamp | Optional. Specify timestamp. The default is the current date and time. |
The first required parameter format of the date() function specifies how to format the date/time.
Here are some available characters:
d - represents the day of the month (01 - 31)
m - represents 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 that you can add additional formatting:
<?phpecho date("Y/m/d") . "<br>";echo date("Ymd") . "<br>";echo date("Ymd");?>
The output of the above code looks like this:
2016/10/212016.10.212016-10-21
format character | illustrate | Return value example |
---|---|---|
day | --- | --- |
d | Day of the month, a 2-digit number with leading zeros | 01 to 31 |
D | Day of the week, text representation, 3 letters | Mon to Sun |
j | Day of the month, without leading zeros | 1 to 31 |
l (lowercase letter "L") | Day of the week, complete text format | Sunday to Saturday |
N | Day of the week represented by numbers in ISO-8601 format (new in PHP 5.1.0) | 1 (for Monday) to 7 (for Sunday) |
S | English suffix after the day of the month, 2 characters | st , nd , rd or th . Can be used with j |
w | Day of the week, expressed as a number | 0 (for Sunday) to 6 (for Saturday) |
z | Day of the year | 0 to 365 |
Week | --- | --- |
W | Week number of the year in ISO-8601 format, each week starts on Monday (new in PHP 4.1.0) | For example: 42 (the 42nd week of the year) |
moon | --- | --- |
F | Month, complete text format, such as January or March | January to December |
m | Month represented as a number, with leading zeros | 01 to 12 |
M | Three-letter abbreviation for month | Jan to Dec |
n | Month as a number, without leading zeros | 1 to 12 |
t | The number of days in a given month | 28 to 31 |
Year | --- | --- |
L | Is it a leap year? | If it is a leap year, it is 1 , otherwise it is 0 |
o | Year number in ISO-8601 format. This is the same value as Y , except that if the ISO week number ( W ) belongs to the previous or next year, that year is used. (New in PHP 5.1.0) | Examples: 1999 or 2003 |
Y | Full 4-digit year | For example: 1999 or 2003 |
y | 2-digit year | For example: 99 or 03 |
time | --- | --- |
a | Lowercase AM and PM values | am or pm |
A | Uppercase AM and PM values | AM or PM |
B | Swatch Internet standard time | 000 to 999 |
g | hour, 12-hour format, no leading zeros | 1 to 12 |
G | hour, 24 hour format, no leading zeros | 0 to 23 |
h | Hours, 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 to 59 > |
s | Seconds, with leading zeros | 00 to 59 > |
u | Milliseconds (new in PHP 5.2.2). It should be noted that the date() function always returns 000000 because it only accepts integer parameters, and DateTime::format() only supports milliseconds. | Example: 654321 |
time zone | --- | --- |
e | Time zone identifier (new in PHP 5.1.0) | For example: UTC , GMT , Atlantic/Azores |
I | Is it daylight saving time? | 1 if it is daylight saving time, 0 otherwise |
O | hours from Greenwich Mean Time | For example: +0200 |
P | The difference from Greenwich Mean Time (GMT), there is a colon separating hours and minutes (new in PHP 5.1.3) | For example: +02:00 |
T | The time zone of this machine | For example: EST , MDT ([Translator's Note] In complete text format under Windows, such as "Eastern Standard Time", the Chinese version will display "China Standard Time"). |
Z | Time difference offset in seconds. Time zone offsets west of UTC are always negative, and time zone offsets east of UTC are 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 format | For example: Thu, 21 Dec 2000 16:01:07 +0200 |
U | Number of seconds since the Unix epoch (January 1 1970 00:00:00 GMT) | See time() |
For a complete reference manual for all date functions, visit our Complete PHP Date Reference Manual.
This reference manual provides a brief description and application examples of each function!