How to prints every single date() formatting token in PHP

1 Answer

0 votes
$now = time();   // Current timestamp

echo "=== PHP DATE() FORMAT TOKENS ===\n\n";

/*
|--------------------------------------------------------------------------
| DAY TOKENS
|--------------------------------------------------------------------------
*/
$formats = [
    // Day
    "d" => "Day of month, 2 digits (01–31)",
    "D" => "Short weekday name (Mon–Sun)",
    "j" => "Day of month without leading zero (1–31)",
    "l" => "Full weekday name (Monday–Sunday)",
    "N" => "ISO-8601 weekday number (1=Mon, 7=Sun)",
    "S" => "English ordinal suffix (st, nd, rd, th)",
    "w" => "Numeric weekday (0=Sun, 6=Sat)",
    "z" => "Day of year (0–365)",
];

foreach ($formats as $f => $desc) {
    echo "$f — $desc: " . date($f, $now) . "\n";
}

echo "\n";

/*
|--------------------------------------------------------------------------
| WEEK TOKENS
|--------------------------------------------------------------------------
*/
$formats = [
    "W" => "ISO-8601 week number of year (01–53)",
];

foreach ($formats as $f => $desc) {
    echo "$f — $desc: " . date($f, $now) . "\n";
}

echo "\n";

/*
|--------------------------------------------------------------------------
| MONTH TOKENS
|--------------------------------------------------------------------------
*/
$formats = [
    "F" => "Full month name (January–December)",
    "m" => "Month number with leading zero (01–12)",
    "M" => "Short month name (Jan–Dec)",
    "n" => "Month number without leading zero (1–12)",
    "t" => "Number of days in month (28–31)",
];

foreach ($formats as $f => $desc) {
    echo "$f — $desc: " . date($f, $now) . "\n";
}

echo "\n";

/*
|--------------------------------------------------------------------------
| YEAR TOKENS
|--------------------------------------------------------------------------
*/
$formats = [
    "L" => "Leap year? (1=yes, 0=no)",
    "o" => "ISO-8601 year number",
    "Y" => "4-digit year",
    "y" => "2-digit year",
];

foreach ($formats as $f => $desc) {
    echo "$f — $desc: " . date($f, $now) . "\n";
}

echo "\n";

/*
|--------------------------------------------------------------------------
| TIME TOKENS
|--------------------------------------------------------------------------
*/
$formats = [
    "a" => "Lowercase am/pm",
    "A" => "Uppercase AM/PM",
    "B" => "Swatch Internet time (000–999)",
    "g" => "12-hour format without leading zero (1–12)",
    "G" => "24-hour format without leading zero (0–23)",
    "h" => "12-hour format with leading zero (01–12)",
    "H" => "24-hour format with leading zero (00–23)",
    "i" => "Minutes (00–59)",
    "s" => "Seconds (00–59)",
    "u" => "Microseconds",
    "v" => "Milliseconds",
];

foreach ($formats as $f => $desc) {
    echo "$f — $desc: " . date($f, $now) . "\n";
}

echo "\n";

/*
|--------------------------------------------------------------------------
| TIMEZONE TOKENS
|--------------------------------------------------------------------------
*/
$formats = [
    "e" => "Timezone identifier (e.g., Europe/London)",
    "I" => "DST? (1=yes, 0=no)",
    "O" => "GMT offset (e.g., +0200)",
    "P" => "GMT offset with colon (e.g., +02:00)",
    "p" => "GMT offset minutes",
    "T" => "Timezone abbreviation (e.g., IST)",
    "Z" => "Timezone offset in seconds",
];

foreach ($formats as $f => $desc) {
    echo "$f — $desc: " . date($f, $now) . "\n";
}

echo "\n";

/*
|--------------------------------------------------------------------------
| FULL DATE/TIME COMPOSITE TOKENS
|--------------------------------------------------------------------------
*/
$formats = [
    "c" => "ISO 8601 datetime",
    "r" => "RFC 2822 formatted date",
    "U" => "Unix timestamp",
];

foreach ($formats as $f => $desc) {
    echo "$f — $desc: " . date($f, $now) . "\n";
}

echo "\n=== END ===\n";



/*
run:

=== PHP DATE() FORMAT TOKENS ===

d — Day of month, 2 digits (01–31): 20
D — Short weekday name (Mon–Sun): Wed
j — Day of month without leading zero (1–31): 20
l — Full weekday name (Monday–Sunday): Wednesday
N — ISO-8601 weekday number (1=Mon, 7=Sun): 3
S — English ordinal suffix (st, nd, rd, th): th
w — Numeric weekday (0=Sun, 6=Sat): 3
z — Day of year (0–365): 139

W — ISO-8601 week number of year (01–53): 21

F — Full month name (January–December): May
m — Month number with leading zero (01–12): 05
M — Short month name (Jan–Dec): May
n — Month number without leading zero (1–12): 5
t — Number of days in month (28–31): 31

L — Leap year? (1=yes, 0=no): 0
o — ISO-8601 year number: 2026
Y — 4-digit year: 2026
y — 2-digit year: 26

a — Lowercase am/pm: pm
A — Uppercase AM/PM: PM
B — Swatch Internet time (000–999): 602
g — 12-hour format without leading zero (1–12): 1
G — 24-hour format without leading zero (0–23): 13
h — 12-hour format with leading zero (01–12): 01
H — 24-hour format with leading zero (00–23): 13
i — Minutes (00–59): 27
s — Seconds (00–59): 56
u — Microseconds: 000000
v — Milliseconds: 000

e — Timezone identifier (e.g., Europe/London): UTC
I — DST? (1=yes, 0=no): 0
O — GMT offset (e.g., +0200): +0000
P — GMT offset with colon (e.g., +02:00): +00:00
p — GMT offset minutes: Z
T — Timezone abbreviation (e.g., IST): UTC
Z — Timezone offset in seconds: 0

c — ISO 8601 datetime: 2026-05-20T13:27:56+00:00
r — RFC 2822 formatted date: Wed, 20 May 2026 13:27:56 +0000
U — Unix timestamp: 1779283676

=== END ===

*/

 



answered 1 day ago by avibootz
...