Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP date() Function

PHP date() Function


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.

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

Syntax



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

Parameters Description
format Required. Specifies the format of the timestamp.
timestamp Optional. Specifies the timestamp. Defaults to the current date and time.

PHP Date() - Formatting a Date

The first required parameter of the date() function format specifies how the date/time should be formatted.

Here are some available characters:

To learn about format the full list of characters available in the parameter, please refer to our PHP Date reference manual,the date() function.

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



";

echo date("Y.m.d") . "
"; echo date("Y-m-d"); ?>

The output of the code above is shown below:



2016/10/21

2016.10.21

2016-10-21

format Character Description Example Return Value
Day --- ---
d Day of the month, 2 digits with leading zeros 01 to 31
D Day of the week, textual, 3 letters Mon to Sun
j Day of the month without leading zeros 1 to 31
l(lowercase "L") Day of the week, full textual format Sunday to Saturday
N Day of the week as a number in ISO-8601 format (added in PHP 5.1.0) 1(representing Monday) to 7(representing Sunday)
S English suffix for the day of the month, 2 characters st, nd, rd or th. Can be used with j together
w Day of the week, numeric 0(representing Sunday) to 6(representing Saturday)
z Day of the year 0 to 365
Week --- ---
W Week number of the year in ISO-8601 format, weeks starting on Monday (added in PHP 4.1.0) For example:42(42nd week of the year)
Month --- ---
F Month, full textual format, e.g. January or March January to December
m Month, numeric, with leading zeros 01 to 12
M Month, abbreviated to 3 letters Jan to Dec
n Month, numeric, without leading zeros 1 to 12
t Number of days in the given month 28 to 31
Year --- ---
L Whether it is a leap year If it is a leap year, it is 1; otherwise, it is 0
o ISO-8601 year number. This is the same as Y 's value, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0) Examples: 1999 or 2003
Y Year, full 4 digits For example:1999 or 2003
y Year, 2 digits For example:99 or 03
Time --- ---
a Lowercase ante meridiem and post meridiem am or pm
A Uppercase ante meridiem and post meridiem AM or PM
B Swatch Internet 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 to 59>
s Seconds, with leading zeros 00 to 59>
u Milliseconds (added in PHP 5.2.2). Note that date() the function always returns 000000 because it only accepts an integer parameter, whereas DateTime::format() supports milliseconds. Example: 654321
Timezone --- ---
e Timezone identifier (added in PHP 5.1.0) For example:UTC, GMT, Atlantic/Azores
I Whether it is daylight saving time If it is daylight saving time, it is 1; otherwise, it is 0
O Difference to Greenwich time (GMT) in hours For example:+0200
P Difference to GMT, with a colon between hours and minutes (added in PHP 5.1.3) For example:+02:00
T The timezone of the machine For example:EST, MDT(Note: on Windows this is the full textual format, e.g. "Eastern Standard Time"; the Chinese version displays "China Standard Time".)
Z Seconds offset of the timezone. Timezones west of UTC always have a negative offset, and timezones east of UTC always have a positive offset. -43200 to 43200
Complete date/time --- ---
c ISO 8601 date (added in PHP 5) 2004-02-12T15:19:21+00:00
r RFC 822 date For example:Thu, 21 Dec 2000 16:01:07 +0200
U Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) See time()

Complete PHP Date Reference Manual

To view the complete reference manual for all date functions, please visit our Complete PHP Date Reference Manual.

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