Dates and Time Concepts
Parse Date
Parsing turns a known string shape into a date object.
known shape
Use an explicit input format when the source string has a fixed shape.
Parse Date
parse_date.php
Replay: real traced execution (multi-file project)
<?php
$raw = "2026-05-31";
$date = DateTimeImmutable::createFromFormat("Y-m-d", $raw, new DateTimeZone("UTC"));
$month = $date->format("m");
$day = $date->format("d");
echo "raw=" . $raw . "\n";
echo "month=" . $month . "\n";
echo "day=" . $day . "\n";
<?php
$raw = "2026-01-15";
$date = DateTimeImmutable::createFromFormat("Y-m-d", $raw, new DateTimeZone("UTC"));
$month = $date->format("m");
$day = $date->format("d");
echo "raw=" . $raw . "\n";
echo "month=" . $month . "\n";
echo "day=" . $day . "\n";
<?php
$raw = "2026-12-25";
$date = DateTimeImmutable::createFromFormat("Y-m-d", $raw, new DateTimeZone("UTC"));
$month = $date->format("m");
$day = $date->format("d");
echo "raw=" . $raw . "\n";
echo "month=" . $month . "\n";
echo "day=" . $day . "\n";
$raw ← 2026-05-31, $date ← DateTimeImmutable, $month ← 05, $day ← 31
1<?php2$raw→ 2026-05-31 = "2026-05-31"; //@raw="2026-01-15", "2026-12-25"3$date→ DateTimeImmutable = DateTimeImmutable::createFromFormat("Y-m-d", $raw2026-05-31, new DateTimeZone("UTC"));4$month→ 05 = $dateDateTimeImmutable->format("m");5$day→ 31 = $dateDateTimeImmutable->format("d");67echo "raw=" . $raw2026-05-31 . "\n";8echo "month=" . $month05 . "\n";9echo "day=" . $day31 . "\n";outputraw=2026-05-31 month=05 day=31
$raw ← 2026-01-15, $date ← DateTimeImmutable, $month ← 01, $day ← 15
1<?php2$raw→ 2026-01-15 = "2026-01-15";3$date→ DateTimeImmutable = DateTimeImmutable::createFromFormat("Y-m-d", $raw2026-01-15, new DateTimeZone("UTC"));4$month→ 01 = $dateDateTimeImmutable->format("m");5$day→ 15 = $dateDateTimeImmutable->format("d");67echo "raw=" . $raw2026-01-15 . "\n";8echo "month=" . $month01 . "\n";9echo "day=" . $day15 . "\n";outputraw=2026-01-15 month=01 day=15
$raw ← 2026-12-25, $date ← DateTimeImmutable, $month ← 12, $day ← 25
1<?php2$raw→ 2026-12-25 = "2026-12-25";3$date→ DateTimeImmutable = DateTimeImmutable::createFromFormat("Y-m-d", $raw2026-12-25, new DateTimeZone("UTC"));4$month→ 12 = $dateDateTimeImmutable->format("m");5$day→ 25 = $dateDateTimeImmutable->format("d");67echo "raw=" . $raw2026-12-25 . "\n";8echo "month=" . $month12 . "\n";9echo "day=" . $day25 . "\n";outputraw=2026-12-25 month=12 day=25