Formatting turns a date object into a string for display or storage.

display style Choose a named style first, then map it to the date format string.

Format Date

style
format_date.php
Replay: real traced execution (multi-file project)
<?php
$style = "iso";
$date = new DateTimeImmutable("2026-05-31", new DateTimeZone("UTC"));

if ($style === "slash") {
    $format = "Y/m/d";
} elseif ($style === "month") {
    $format = "M d";
} else {
    $format = "Y-m-d";
}

$label = $date->format($format);

echo "style=" . $style . "\n";
echo "label=" . $label . "\n";
<?php
$style = "slash";
$date = new DateTimeImmutable("2026-05-31", new DateTimeZone("UTC"));

if ($style === "slash") {
    $format = "Y/m/d";
} elseif ($style === "month") {
    $format = "M d";
} else {
    $format = "Y-m-d";
}

$label = $date->format($format);

echo "style=" . $style . "\n";
echo "label=" . $label . "\n";
<?php
$style = "month";
$date = new DateTimeImmutable("2026-05-31", new DateTimeZone("UTC"));

if ($style === "slash") {
    $format = "Y/m/d";
} elseif ($style === "month") {
    $format = "M d";
} else {
    $format = "Y-m-d";
}

$label = $date->format($format);

echo "style=" . $style . "\n";
echo "label=" . $label . "\n";
  1. $style ← iso, $date ← DateTimeImmutable, $format ← Y-m-d, $label ← 2026-05-31

    1<?php2$style→ iso = "iso"; //@style="slash", "month"3$date→ DateTimeImmutable = new DateTimeImmutable("2026-05-31", new DateTimeZone("UTC"));45if ($style === "slash") {6    $format = "Y/m/d";7} elseif ($style === "month") {8    $format = "M d";9} else {10    $format→ Y-m-d = "Y-m-d";11}1213$label→ 2026-05-31 = $dateDateTimeImmutable->format($formatY-m-d);1415echo "style=" . $styleiso . "\n";16echo "label=" . $label2026-05-31 . "\n";
    outputstyle=iso
    label=2026-05-31
  1. $style ← slash, $date ← DateTimeImmutable

    1<?php2$style→ slash = "slash";3$date→ DateTimeImmutable = new DateTimeImmutable("2026-05-31", new DateTimeZone("UTC"));
  2. $format ← Y/m/d

    5if ($styleslash === "slash") {6    $format→ Y/m/d = "Y/m/d";7} elseif ($style === "month") {
  3. $label ← 2026/05/31

    13$label→ 2026/05/31 = $dateDateTimeImmutable->format($formatY/m/d);1415echo "style=" . $styleslash . "\n";16echo "label=" . $label2026/05/31 . "\n";
    outputstyle=slash
    label=2026/05/31
  1. $style ← month, $date ← DateTimeImmutable, $format ← M d, $label ← May 31

    1<?php2$style→ month = "month";3$date→ DateTimeImmutable = new DateTimeImmutable("2026-05-31", new DateTimeZone("UTC"));45if ($style === "slash") {6    $format = "Y/m/d";7} elseif ($style === "month") {8    $format→ M d = "M d";9} else {10    $format = "Y-m-d";11}1213$label→ May 31 = $dateDateTimeImmutable->format($formatM d);1415echo "style=" . $stylemonth . "\n";16echo "label=" . $labelMay 31 . "\n";
    outputstyle=month
    label=May 31