Skip to content

← Writing

engineering

The PHP date() Function: Common Formats

· Jerwin Arnado · 5 min read ·

date() is one of those PHP functions I’ve typed a thousand times and still occasionally look up, because nobody actually remembers whether the day is d or D or j. So here’s the reference I wish I could paste straight from memory: the format characters that matter, and the common patterns built from them.

The signature is simple:

date(string $format, ?int $timestamp = null): string

Pass a format string; leave the second argument off and it uses “now.” Pass a Unix timestamp and it formats that instant instead.

The Characters Worth Knowing

date() has dozens of format characters, but you’ll reach for the same handful constantly. These are the ones to actually memorize:

Char Means Example
Y 4-digit year 2026
y 2-digit year 26
m Month, zero-padded 07
n Month, no padding 7
F Month name, full July
M Month name, short Jul
d Day, zero-padded 06
j Day, no padding 6
l Weekday, full Monday
D Weekday, short Mon
H Hour, 24h zero-padded 14
G Hour, 24h no padding 14
h Hour, 12h zero-padded 02
g Hour, 12h no padding 2
i Minutes, zero-padded 09
s Seconds, zero-padded 05
A / a AM/PM vs am/pm PM / pm

The pattern to internalize: lowercase tends to mean short/unpadded, uppercase means long/paddedn vs m, j vs d, g vs h. Not a perfect rule, but it gets you most of the way.

Common Patterns

The formats I paste most often, ready to go:

// ISO-style date — great for filenames, sorting, DB-friendly output
date('Y-m-d');              // 2026-07-06

// 24-hour timestamp
date('Y-m-d H:i:s');        // 2026-07-06 14:09:05

// Human-readable date
date('F j, Y');             // July 6, 2026

// Weekday + date
date('l, F j, Y');          // Monday, July 6, 2026

// 12-hour time with AM/PM
date('g:i A');              // 2:09 PM

// Short, log-line style
date('M j, H:i');           // Jul 6, 14:09

// Full ISO 8601 (there's a constant for this — see below)
date('c');                  // 2026-07-06T14:09:05+08:00

Escaping Literal Letters

The gotcha that bites everyone: any letter in the format string that happens to be a format character will be interpreted. Want to write the word “at” between a date and time? The a becomes am/pm and the t becomes the days-in-month. Escape literal letters with a backslash:

date('l \a\t g:i A');       // Monday at 2:09 PM

Spaces, commas, colons, and slashes are safe as-is — only letters need escaping.

Use the Built-In Constants

For machine-readable output — API responses, feeds, Date: headers — don’t hand-roll the format. PHP ships constants for the standard ones:

date(DATE_ATOM);            // 2026-07-06T14:09:05+08:00  (ISO 8601, same as 'c')
date(DATE_RFC2822);         // Mon, 06 Jul 2026 14:09:05 +0800
date(DATE_COOKIE);          // Monday, 06-Jul-2026 14:09:05 UTC

The Timezone Gotcha

date() formats against PHP’s default timezone, not necessarily the one you want. If output is coming out shifted by hours, that’s almost always why. Set it explicitly, once, near your app’s entry point:

date_default_timezone_set('Asia/Manila');

Better still on any modern codebase: skip date() for anything timezone-sensitive and reach for DateTimeImmutable, which carries its own timezone and won’t mutate out from under you:

$now = new DateTimeImmutable('now', new DateTimeZone('Asia/Manila'));
echo $now->format('Y-m-d H:i:s');   // same format characters, safer object

The nice part: ->format() takes the exact same format strings as date(). So everything in the table above still applies — you just get proper timezone handling and immutability for free. Learn the characters once, use them everywhere.