Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Date and Time

PHP date format characters

Reference for PHP date format characters used by date(), DateTime::format() and createFromFormat(): day, month, year, time, zone and escaping rules with examples.

A date format is a string of single-letter codes that PHP replaces with parts of a date and time. The same letters work in date(), DateTime::format(), DateTimeImmutable::format() and date_create_from_format() (with a few extra parsing-only letters). Any character that is not a code is copied to the output unchanged, and a backslash escapes a letter you want to print literally.

Learning the handful of codes you use daily (Y-m-d H:i:s, D, d M Y, g:i A) covers most needs. The rest of this page is the complete list so you can look up the odd one.

PHP
$d = new DateTimeImmutable('2024-03-09 14:05:07', new DateTimeZone('UTC'));
echo $d->format('D, d M Y H:i'); // Sat, 09 Mar 2024 14:05

Try it

PHP Common formats side by side

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Escaping and ready-made constants

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Day characters

Code Meaning Example
d Day of the month, two digits 01 to 31
j Day of the month, no leading zero 1 to 31
D Short day name (English) Mon to Sun
l (lowercase L) Full day name Monday
N ISO day of the week, Monday is 1 1 to 7
w Day of the week, Sunday is 0 0 to 6
S English ordinal suffix; use after j st, nd, rd, th
z Day of the year, counting from 0 0 to 365

Week, month and year characters

Code Meaning Example
W ISO 8601 week number, two digits 01 to 53
F Full month name January
M Short month name Jan
m Month, two digits 01 to 12
n Month, no leading zero 1 to 12
t Days in the month 28 to 31
L Leap year flag 1 or 0
o ISO 8601 week-numbering year 2024
Y Year, at least four digits 2024
y Year, two digits 24

Time characters

Code Meaning Example
a / A Lowercase or uppercase am/pm pm / PM
g / h 12-hour clock, no padding / padded 2 / 02
G / H 24-hour clock, no padding / padded 14 / 14
i Minutes, padded 00 to 59
s Seconds, padded 00 to 59
u Microseconds, six digits 123456
v Milliseconds, three digits 123
B Swatch Internet time 000 to 999

Time zone characters

Code Meaning Example
e Time zone identifier Europe/Paris
T Abbreviation (or offset when none exists) CET
P Offset with a colon +01:00
p Like P, but Z for UTC Z
O Offset without a colon +0100
Z Offset in seconds 3600
I (capital i) Daylight saving flag 1 or 0

Full date/time and escaping

Code Meaning
c ISO 8601 date, for example 2024-03-09T14:05:07+00:00
r RFC 2822 date, for example Sat, 09 Mar 2024 14:05:07 +0000
U Seconds since the Unix epoch
\ Escapes the next character, so \T prints a literal T

In a PHP string, write the backslash correctly. In single quotes '\T' already works because \T is not an escape; in double quotes you need "\\T". Use single quotes for format strings.

Usage notes

  • Case matters. m is month and i is minutes; M is the month name. H and h differ (24-hour and 12-hour), and s is seconds while S is the ordinal suffix.
  • Unescaped letters are expanded. format('Today is ...') turns T, o, d, a, y, i, s into values. Escape each literal letter, or build the text in pieces.
  • Predefined constants such as DATE_ATOM, DATE_RFC2822, DATE_COOKIE and DateTimeInterface::ATOM hold ready-made formats. Prefer them for protocols over hand-typed strings.
  • Y-m-d\TH:i:sP is the format JSON APIs usually want; DATE_ATOM is the same string. c is equivalent for most purposes.
  • o versus Y: around New Year the ISO week year can differ from the calendar year (December 31 may be week 1 of next year). Pair W with o, not Y.
  • u with date(): date() takes an integer timestamp, so u is always 000000. Use DateTime objects for real microseconds.
  • Names are English only. format() does not translate day or month names; use IntlDateFormatter (the intl extension) for localized output.
  • Parsing: with createFromFormat() the extra codes ! and | reset unparsed fields, ? skips one character, and * skips until a separator. Unparsed fields default to the current time unless you use these.
  • Errors in a format never throw; unknown characters are copied literally.