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

Format Date

format_date.php
<?php
$style = ;
$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 = ;
$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 = ;
$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";
display style Choose a named style first, then map it to the date format string.