PHP Date Format Examples & Cheat Sheet (2026)

A stylized flat vector calendar page enclosed in code brackets. Text: PHP DATE FORMAT.

PHP’s date() format characters are impossible to memorize: is the 4-digit year Y or y, is the month m or M? This DevAnswers cheat sheet is the lookup table plus copy-paste snippets for every common pattern, and it points you at the modern DateTimeImmutable tools for the jobs date() handles badly: parsing, arithmetic, timezones, and localized output. You can also open the full-page PHP date format builder to bookmark or share it.

PHP date() Format Builder Build a PHP date() format string visually, preview it live, and copy ready-to-paste code.
Output

Escape literal letters with a backslash, e.g. l \t\h\e jS → “Wednesday the 7th”.

Format Character Reference Table 

The first argument to date() is a format string of single-character codes, and DateTimeInterface::format() (used by DateTime and DateTimeImmutable) reads exactly the same characters. Combine them freely with literal punctuation: date("l, F j") gives Sunday, March 8. Case matters and is not consistent, so the table below is the part worth bookmarking.

CharMeaningExample
Day
dDay of month, leading zero01 to 31
jDay of month, no leading zero1 to 31
DDay name, shortMon to Sun
lDay name, fullSunday to Saturday
NISO weekday number (Mon=1)1 to 7
wWeekday number (Sun=0)0 to 6
SOrdinal suffix (pair with j)st, nd, rd, th
Month
mMonth, leading zero01 to 12
nMonth, no leading zero1 to 12
FMonth name, fullJanuary to December
MMonth name, shortJan to Dec
tDays in this month28 to 31
Year
YYear, 4 digits2026
yYear, 2 digits26
LLeap year flag1 or 0
Time
HHour, 24-hour, leading zero00 to 23
GHour, 24-hour, no leading zero0 to 23
hHour, 12-hour, leading zero01 to 12
gHour, 12-hour, no leading zero1 to 12
iMinutes, leading zero00 to 59
sSeconds, leading zero00 to 59
A / aAM/PM, upper / lowerAM, PM / am, pm
Whole date / time
cISO 86012026-03-08T14:30:00+00:00
rRFC 2822Sun, 08 Mar 2026 14:30:00 +0000
UUnix timestamp (seconds)1772980200
Timezone
POffset with colon+00:00
OOffset without colon+0000
e / TTimezone name / abbreviationUTC / GMT

For the full list, including the rarer characters, see the official PHP date format reference.

Copy-Paste Examples by Use Case 

Each snippet below pins its date to a fixed moment, 2026-03-08 14:30:00 UTC, so the output is identical on any machine (the timezone is set explicitly because the c, r, and U formats depend on it). Swap in new DateTimeImmutable() with no arguments for the current time in your default zone. The examples are grouped by goal so you can jump to the pattern you need.

Database and API Formats 

PHP
$d = new DateTimeImmutable('2026-03-08 14:30:00', new DateTimeZone('UTC'));

echo $d->format('Y-m-d H:i:s');  // 2026-03-08 14:30:00   (MySQL DATETIME)
echo $d->format('Y-m-d');        // 2026-03-08             (MySQL DATE)
echo $d->format('c');            // 2026-03-08T14:30:00+00:00  (ISO 8601 / JSON)
echo $d->format('r');            // Sun, 08 Mar 2026 14:30:00 +0000  (RFC 2822)
echo $d->format('U');            // 1772980200             (Unix timestamp)

The Y-m-d H:i:s shape matches a MySQL DATETIME column exactly, so it is the format to bind when you insert with prepared statements or PDO. For anything crossing a network, prefer c (ISO 8601): it carries the timezone offset, so the receiver never has to guess.

Human-Readable Formats 

PHP
$d = new DateTimeImmutable('2026-03-08 14:30:00', new DateTimeZone('UTC'));

echo $d->format('m/d/Y');      // 03/08/2026   (US)
echo $d->format('d/m/Y');      // 08/03/2026   (Europe)
echo $d->format('F j, Y');     // March 8, 2026
echo $d->format('l, jS F Y');  // Sunday, 8th March 2026
echo $d->format('g:i A');      // 2:30 PM
echo $d->format('H:i');        // 14:30

The US and European outputs differ here (03/08/2026 vs 08/03/2026), but the real trap is the reverse direction: an incoming string like 05/06/2026 is ambiguous, since you cannot tell 6 May from 5 June without knowing which format produced it. That ambiguity is the single most common date bug in PHP, and the parsing section below is how you defuse it.

Parsing Dates with createFromFormat() 

Formatting is easy because you control the output. Parsing is the hard direction: a string arrives from a form, a CSV, or an API and you have to turn it into a real date. The quick tool is strtotime(), but it guesses, and it guesses from the separator.

strtotime() reads a slash as US month/day/year, so strtotime('08/07/2026') is August 7. It reads a dash or dot as day-month-year, so strtotime('08-07-2026') is 8 July. The one exception is the ISO YYYY-MM-DD shape (2026-08-07), which is correctly read year-month-day. Never let strtotime() guess the format of input you do not control.

DateTimeImmutable::createFromFormat() removes the guessing by making you state the exact format you expect:

PHP
// Tell PHP the input is day/month/year, no guessing
$eu = DateTimeImmutable::createFromFormat('d/m/Y', '08/07/2026');
echo $eu->format('F j, Y');  // July 8, 2026

// Same digits, parsed as month/day/year
$us = DateTimeImmutable::createFromFormat('m/d/Y', '08/07/2026');
echo $us->format('F j, Y');  // August 7, 2026

Always check the result. createFromFormat() returns false on a total parse failure, but it will also quietly overflow an impossible date like 30 February into early March instead of rejecting it. DateTimeImmutable::getLastErrors() reports that overflow as a warning so you can catch it:

PHP
$date = DateTimeImmutable::createFromFormat('Y-m-d', '2026-02-30');
echo $date->format('Y-m-d');  // 2026-03-02  (silently overflowed!)

$errors = DateTimeImmutable::getLastErrors();
echo $errors['warning_count'];        // 1
echo current($errors['warnings']);    // The parsed date was invalid
Append | to the format string ('Y-m-d|') to zero out any fields the input does not provide. Without it, parsing a bare date leaves the time set to the moment the script ran, which is a classic source of off-by-a-few-hours test failures.

Date Math with strtotime() and DateInterval 

For a throwaway calculation, strtotime() reads plain-English offsets and returns a Unix timestamp. The optional second argument is the base time to count from:

PHP
$base = strtotime('2026-01-01');
echo date('Y-m-d', strtotime('+90 days', $base));  // 2026-04-01
echo date('Y-m-d', strtotime('last day of February 2026', $base));  // 2026-02-28

For anything you keep, use DateTimeImmutable with DateInterval. It preserves the timezone that strtotime() throws away, and diff() hands back a structured DateInterval instead of a raw second count. Intervals are written as ISO 8601 durations: P for period, then the amount, so P90D is 90 days and P2M is two months.

PHP
$start = new DateTimeImmutable('2026-01-15');

echo $start->add(new DateInterval('P90D'))->format('Y-m-d');  // 2026-04-15
echo $start->sub(new DateInterval('P2M'))->format('Y-m-d');   // 2025-11-15

$diff = $start->diff(new DateTimeImmutable('2026-12-25'));
echo $diff->days . ' days';  // 344 days
Adding months is where date math bites. strtotime('+1 month') from 31 January lands on 3 March, because PHP rolls the month to a nonexistent 31 February and lets it overflow three days into March. When you need real calendar months, work from the first of the month or use DateTimeImmutable::modify('first day of next month').

A “Time Ago” Helper 

Activity feeds and comment threads usually want 3 hours ago rather than a full timestamp, and PHP has no built-in for it. This helper takes a Unix timestamp and walks from the largest unit down, returning the first one that fits. Anything under a minute collapses to just now so you never show 43 seconds ago:

PHP
function timeAgo(int $timestamp): string
{
    $seconds = time() - $timestamp;
    if ($seconds < 60) {
        return 'just now';
    }

    $units = ['year' => 31536000, 'month' => 2592000, 'week' => 604800,
              'day' => 86400, 'hour' => 3600, 'minute' => 60];

    foreach ($units as $name => $size) {
        $count = intdiv($seconds, $size);
        if ($count >= 1) {
            return $count . ' ' . $name . ($count > 1 ? 's' : '') . ' ago';
        }
    }
    return 'just now';
}

echo timeAgo(time() - 3600);    // 1 hour ago
echo timeAgo(time() - 172800);  // 2 days ago
echo timeAgo(time() - 30);      // just now

The month and year buckets use rounded averages (30 and 365 days), which is the accepted trade-off for relative phrasing. When you need exact calendar differences instead, build a DateInterval with diff() as shown above rather than dividing raw seconds.

Localized, Non-English Dates with IntlDateFormatter 

date() always prints English month and day names, whatever the server locale. For translated dates, the modern, correct tool is IntlDateFormatter from the intl extension, which draws on the ICU locale library and handles grammar that simple string swaps cannot:

PHP
$d = new DateTimeImmutable('2026-03-08');

$fr = new IntlDateFormatter('fr_FR', IntlDateFormatter::LONG, IntlDateFormatter::NONE);
echo $fr->format($d);  // 8 mars 2026

$de = new IntlDateFormatter('de_DE', IntlDateFormatter::LONG, IntlDateFormatter::NONE);
echo $de->format($d);  // 8. März 2026

$es = new IntlDateFormatter('es_ES', IntlDateFormatter::FULL, IntlDateFormatter::NONE);
echo $es->format($d);  // domingo, 8 de marzo de 2026

The second and third arguments are the date and time styles: FULL, LONG, MEDIUM, or SHORT, with NONE to drop one of the two parts (the fourth argument is the timezone). The intl extension ships with most PHP builds but is occasionally disabled; confirm it portably with php -r "var_dump(extension_loaded('intl'));", which works on every OS unlike a grep pipe.

If you still localize dates with setlocale() and strftime(), migrate now: strftime() was deprecated in PHP 8.1 and is scheduled for removal in PHP 9.0. The path off it is the IntlDateFormatter shown here; see our guide to fixing setlocale and strftime issues.

Predefined Format Constants 

PHP names the common machine formats as constants, which read better than the raw strings and signal intent to the next developer:

PHP
$d = new DateTimeImmutable('2026-03-08 14:30:00', new DateTimeZone('UTC'));

echo $d->format(DATE_ATOM);     // 2026-03-08T14:30:00+00:00  (same as 'c')
echo $d->format(DATE_RFC2822);  // Sun, 08 Mar 2026 14:30:00 +0000
echo $d->format(DATE_COOKIE);   // Sunday, 08-Mar-2026 14:30:00 UTC
Despite the name, DATE_ISO8601 is not strictly ISO 8601: it is defined as Y-m-d\TH:i:sO, so it emits the offset as +0000 (the O character) instead of +00:00 (P). Use DATE_ATOM, or the c character, when you need a parser-friendly ISO 8601 string.

DateTime vs date(): Pick the Object 

Procedural date() and the object methods share the same format characters, so output is identical. The difference is everything around formatting. date() works on integer timestamps, which discard timezone and force you to convert back and forth for arithmetic. The object wraps the moment, its timezone, and formatting together, and timezone conversion is one call:

PHP
$utc = new DateTimeImmutable('2026-06-15 14:30', new DateTimeZone('UTC'));
$ny  = $utc->setTimezone(new DateTimeZone('America/New_York'));

echo $utc->format('H:i T');  // 14:30 UTC
echo $ny->format('H:i T');   // 10:30 EDT

Reach for DateTimeImmutable specifically, not DateTime. DateTime is mutable: calling modify() on it changes the original object in place, which silently corrupts any date shared between functions or stored in an array. DateTimeImmutable returns a new object and leaves the original untouched:

PHP
$mut = new DateTime('2026-03-08');
$mut->modify('+1 day');
echo $mut->format('Y-m-d');  // 2026-03-09  (original changed)

$imm = new DateTimeImmutable('2026-03-08');
$next = $imm->modify('+1 day');
echo $imm->format('Y-m-d');   // 2026-03-08  (original preserved)
echo $next->format('Y-m-d');  // 2026-03-09  (new object)

Escaping Literal Characters 

If your format string contains a letter that is also a format character, PHP will expand it. Backslash the letter to keep it literal, which is how you insert words like at or on:

PHP
$d = new DateTimeImmutable('2026-03-08 14:30:00');

echo $d->format('M j \a\t g:i A');  // Mar 8 at 2:30 PM
Always use single-quoted format strings when they contain literal letters. In a double-quoted string PHP interprets its own escapes before date() ever sees them, so \t becomes a real tab and a string like "\a\t" prints a tab instead of an escaped t. Single quotes pass the backslashes through untouched, which is exactly what date() needs.

Setting the Default Timezone 

date() and any date object created without an explicit timezone format in the script’s default timezone (an object built with its own DateTimeZone uses that instead). When date.timezone is not configured, modern PHP (7.0 and later) falls back to UTC silently. Older PHP raised an E_WARNING on every call for the missing setting, but that warning was removed, so its absence is not a sign the timezone is correct. Set it explicitly so your dates are not quietly off:

PHP
date_default_timezone_set('America/New_York');
echo (new DateTimeImmutable('2026-07-04 12:00', new DateTimeZone('UTC')))
        ->setTimezone(new DateTimeZone(date_default_timezone_get()))
        ->format('H:i T');  // 08:00 EDT

Set it per-script with date_default_timezone_set() for a one-off, or globally with the date.timezone directive in your php.ini file, which is better because it covers every script without code changes. That fallback is UTC, not the server’s own clock, so the two are separate settings: if you want PHP, cron, and your system logs all reading the same zone, set the OS timezone as well, which our guide to configuring timezones on Ubuntu walks through. For apps serving many regions, store everything in UTC and convert to the user’s zone only at display time with setTimezone(). An invalid timezone name still raises a warning, so if you suspect a misconfiguration, turn on error display to see it.

Conclusion 

Keep the character table for quick formatting, but reach for DateTimeImmutable the moment you need to parse, do arithmetic, or cross a timezone, use createFromFormat() over strtotime() for any input you do not control, and prefer IntlDateFormatter over the deprecated strftime() for translated output. Formatting dates in a shell instead of PHP? See the Bash date format reference for the equivalent codes.

Support DevAnswers
Written, tested and maintained by one developer. Your support keeps these guides accurate and ad-light.
Buy me a coffee

Comments

Leave a reply