Skip to content
PHP

PHP Date Formats

5 min read

PHP offers a couple of ways to format a local time/date: date() and strftime(). The first is possibly the most commonly used. date() offers many ways to format a date as well as providing useful formatting parameters for things like identifying a leap year. strftime() will format a local time/date according to locale settings so is useful when dealing with languages.

Both functions use similar formatting parameters (apart from the fact that strftime() needs a % in front of the parameter). There are some differences though (and in some cases no equivalent). For example:-

echo strftime('%Y-%m-%d %H:%M:%S'); // returns '2014-07-02 21:02:00'
echo date('Y-m-d H:i:s'); // returns '2014-07-02 21:02:00'

There have been a few occasions recently where I’ve needed to switch from using one to the other. While both methods are well documented it can be frustrating working out the equivalent parameters (or identifying where a parameter is not available in the other method). To help, I’ve put together the comparison tables below.

Day

date strftime Return Value
D %a Mon through Sun
l %A Monday through Sunday
d %d 01 to 31
j %e 1 to 31
%j 001 to 366 (day of the year)
z 0 to 365 (day of the year)
N %u 1 (for Monday) through 7 (for Sunday)
w %w 0 (for Sunday) through 6 (for Saturday)
S st, nd, rd or th (English ordinal suffix for the day of the month)

Week

date strftime Return Value
%U e.g. 13 (for the 13th full week of the year, starting with the first Sunday as the first week)
%V 01 through 53 (week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week)
W %W e.g. 42 (for the 42nd full week of the year, starting with the first Monday as the first week)

Month

date strftime Return Value
M %b or %h Jan through Dec
F %B January through December
m %m 01 (for January) through 12 (for December)
n 1 (for January) through 12 (for December)
t 28 through 31 (number of days in month)

Year

date strftime Return Value
%C Two digit representation of the century, e.g. 19 for 20th Century
%g Two digit representation of the year going by ISO-8601:1988 standards
o %G The full four-digit version of %g
y %y Two digit representation of the year, e.g. 14 for 2014
Y %Y Four digit representation of the year, e.g. 2014
L Whether it’s a leap year: 1 if it is a leap year, 0 otherwise

Time

date strftime Return Value
Hours
H %H 00 through 23 (two digit representation of the hour in 24-hour format)
%k 0 through 23 (two digit representation of the hour in 24-hour format, with a space preceding single digits)
G 1 through 23 (hour in 24-hour format)
h %I 01 through 23 (two digit representation of the hour in 12-hour format)
g %l 1 through 12 (hour in 12-hour format)
a %P am or pm
A %p AM or PM
Minutes
i %M 00 through 59 (minutes)
Seconds
s %S 00 through 59 (seconds)
u Microseconds, e.g. 654321
U %s Unix Epoch Time timestamp (same as the time() function)
Swatch Internet Time
B 000 through 999

Timezone

date strftime Return Value
O %z Timezone offset, e.g. -0500 for US Eastern Time
P Timezone offset, e.g. -05:00 for US Eastern Time
T %Z Timezone abbreviation, e.g. EST for US Eastern Time
e e.g. UTC, GMT or Atlantic/Azores
I 1 if Daylight Saving Time, 0 otherwise
Z Timezone offset in seconds

Full Date/Time

date strftime Return Value
Time
%r Same as %I:%M:%S %p, e.g. 09:34:17 PM
%R Same as %H:%M, e.g. 21:34
%X Preferred time representation based on locale, e.g. 03:59:16 or 15:59:16
Date
%D Same as %m/%d/%y, e.g. 06/29/14
%F Same as %Y-%m-%d, e.g. 2014-06-29
%x Preferred date representation based on locale, e.g. 06/29/14
Date and Time
%c Preferred date and time stamp based on locale, e.g. Tue Feb 5 00:45:10 2009
c ISO 8601 date, e.g. 2004-02-12T15:19:21+00:00
r RFC 2822 formatted date, e.g. Thu, 21 Dec 2000 16:01:07 +0200

Miscellaneous

date strftime Return Value
%n A new line character (\n)
%t A tab character (\t)
% %% A literal percentage character (%)

Full Documentation

You’ll find the full documentation for these date formats and the date() and strftime() functions on the PHP.net website:-

© 2024 Andy Carter