Formatting a Date

While the Unix timestamp is programmatically useful, it isn't a convenient display format. The date( ) and gmdate( ) functions return a human-readable formatted date and time:

string date(string format [, integer timestamp])
string gmdate(string format [, integer timestamp])

The format of the returned string is determined by the format argument. A predetermined date can be formatted by passing in the optional timestamp argument. Otherwise, both functions format the current time. The format string uses the formatting characters listed in Table 2-3 to display various components or characteristics of the timestamp. To include the characters from the table, the backslash character is used. The following examples show various combinations:

// Set up a timestamp for 08:15am 24 Aug 1964
$var = mktime(8, 15, 25, 8, 24, 1964);
// "24/08/1964"
echo date('d/m/Y', $var);
// "08/24/64"
echo date('m/d/y', $var);
// "Born on Thursday 24th of August"
echo date('\B\o\r\n \o\n l jS \of F", $var);
Table 2-3. Formatting characters that represent various date and time components
Formatting character Meaning
a, A "am" or "pm"; "AM" or "PM"
S Two-character English ordinal suffix: "st", "nd", "rd", "th"
d, j Day of the month: with leading zeros: "01"; without: "1"
D, l Day of the week: as three letters: "Mon"; spelled out: "Monday"
M, F Month: as three letters: "Jan"; spelled out: "January"
m, n Month: with leading zeros: "01"-"12"; without: "1"-"12"
h, g Hour, 12-hour format: with leading zeros: "09"; without: "9"
H, G Hour, 24-hour format: with leading zeros: "01"; without "1"
i Minutes:"00" to "59"
s Seconds: "00" to "59"
Y, y Year: four digits "2002"; two digits "02"
r RFC-2822 formatted date: e.g., "Tue, 29 Jan 2002 09:15:33 +1000" (added in PHP 4.0.4)
w Day of the week as number: "0" (Sunday) to "6" (Saturday)
t Days in the month: "28" to "31"
z Days in the year: "0" to "365"
B Swatch Internet time
L Leap year: "0" for normal year; "1" for leap-year
I Daylight savings time: "0" for standard time; "1" for daylight savings
O Difference to Greenwich Mean Time in hours: "+0200"
T Time zone setting of this machine
Z Time zone offset in seconds: "-43200" to "43200"
U Seconds since the epoch: 00:00:00 1/1/1970

PHP also provides the equivalent functions:

string strftime(string format [, integer timestamp])
string gmstrftime(string format [, integer timestamp])

The format string uses the same formatting character sequences as the C library function strftime( ).

by BrainBellupdated