Latest web development tutorials

PHP date () function

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


PHP date () function

PHP date () function returns the timestamp format more readable date and time.

Tip Timestamp is a sequence of characters representing the date / time of the occurrence of certain events.

grammar

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

parameter description
format Required. Provisions timestamp format.
timestamp Optional. Provisions timestamp. The default is the current date and time.


PHP Date () - Date Format

date () function first required argumentformatspecifies how to format the date / time.

Here are some of the available characters:

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

For a list of all available characterformatparameters, please consult our PHP Date reference manual, a Date () function .

Other characters can be inserted between the letters, such as "/" or "-" so you can add additional formatting the ".":

<?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 as follows:

2016/05/11<br>
2016.05.11<br>
2016-05-11
Format string recognizes the following format parameter string
format character Explanation Example returned values
day --- ---
d The first few days of the month, with leading zeros 2 digits 01-31
D The first few days of the week, textual representation, three letters Mon to Sun
j The first few days of the month without leading zeros 1-31
l ( "L" in lowercase) Several complete text formatting week Sunday to Saturday
N ISO-8601 format digital representation of the first few days of the week (PHP 5.1.0 added) 1 (for Monday) through 7 (for Sunday)
S Days later the English suffix month, 2 characters st, nd, rd or th. And j can be used together
w The first few days of the week, the digital representation 0 (for Sunday) through 6 (for Saturday)
z Year in the first few days 0-365
week --- ---
W ISO-8601 format in the first weeks of the year, a week starting on Monday (PHP 4.1.0 newly added) Example: 42 (the first 42 weeks of the year)
month --- ---
F Month, the full text format, such as January or March January to December
m Month numerals with leading zeros 01-12
M Month represented by three-letter abbreviation Jan to Dec
n Month digital representation without leading zeros 1-12
t To the number of days in a given month should have 28-31
year --- ---
L It is a leap year If it is a leap year is 1, 0 otherwise
o ISO-8601 format of year. This same value as Y, except that if the ISO number (W) belongs to the previous week or next year, that year is used. (PHP 5.1.0 added) Examples: 1999 or 2003
Y Year 4-digit full representation For example: 1999 or 2003
y Two digits of the year For example: 99 or 03
time --- ---
a Lowercase morning and afternoon values am or pm
A Morning and afternoon capitalized value AM or PM
B When Swatch Internet standard 000-999
g Hour, 12-hour format, without leading zeros 1-12
G Hour, 24-hour format, without leading zeros 0-23
h Hour, 12-hour format with leading zeros 01-12
H Hour, 24-hour format with leading zeros 00-23
i There are a few minutes leading zeros 00-59>
s Seconds, with leading zeros 00-59>
u Ms (PHP 5.2.2 added). Note that the date () function always returns 000000 because it only accepts integer arguments, and DateTime :: format () is supported ms. Example: 654321
Time zone --- ---
e Time zone identifier (PHP 5.1.0 added) For example: UTC, GMT, Atlantic / Azores
I Whether daylight saving time If daylight saving time is 1, 0 otherwise
O And the number of hours difference GMT For example: +0200
P Greenwich Mean Time (GMT) of the difference between the hours and minutes separated by a colon (PHP 5.1.3 added) For example: 02: 00
T Time zone where the machine For example: EST, MDT ([Translator's Note] under Windows as full text format, such as "Eastern Standard Time", Chinese version will show "China Standard Time").
Z Offset the difference of seconds. West of UTC time zone offset is always negative, east time zone offset UTC is always positive. -43200 To 43200
Complete date / time --- ---
c ISO 8601 date format (PHP 5 added) 2004-02-12T15: 19: 21 + 00: 00
r RFC 822 date format For example: Thu, 21 Dec 2000 16:01:07 +0200
U Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) start date See also time ()

Complete PHP Date Reference

For a complete reference manual for all date functions, please visit our complete PHP Date Reference Manual .

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