Dates and Time Concepts
Add Days
Date intervals add or subtract calendar amounts from a date.
interval
`DateInterval` describes the change, and `add` returns the adjusted date.
Add Days
add_days.php
Replay: real traced execution (multi-file project)
<?php
$days = 7;
$start = new DateTimeImmutable("2026-05-31", new DateTimeZone("UTC"));
$end = $start->add(new DateInterval("P" . $days . "D"));
echo "days=" . $days . "\n";
echo "end=" . $end->format("Y-m-d") . "\n";
<?php
$days = 1;
$start = new DateTimeImmutable("2026-05-31", new DateTimeZone("UTC"));
$end = $start->add(new DateInterval("P" . $days . "D"));
echo "days=" . $days . "\n";
echo "end=" . $end->format("Y-m-d") . "\n";
<?php
$days = 30;
$start = new DateTimeImmutable("2026-05-31", new DateTimeZone("UTC"));
$end = $start->add(new DateInterval("P" . $days . "D"));
echo "days=" . $days . "\n";
echo "end=" . $end->format("Y-m-d") . "\n";
$days ← 7, $start ← DateTimeImmutable, $end ← DateTimeImmutable
1<?php2$days→ 7 = 7; //@days=1, 303$start→ DateTimeImmutable = new DateTimeImmutable("2026-05-31", new DateTimeZone("UTC"));4$end→ DateTimeImmutable = $startDateTimeImmutable->add(new DateInterval("P" . $days7 . "D"));56echo "days=" . $days7 . "\n";7echo "end=" . $endDateTimeImmutable->format("Y-m-d") . "\n";outputdays=7 end=2026-06-07
$days ← 1, $start ← DateTimeImmutable, $end ← DateTimeImmutable
1<?php2$days→ 1 = 1;3$start→ DateTimeImmutable = new DateTimeImmutable("2026-05-31", new DateTimeZone("UTC"));4$end→ DateTimeImmutable = $startDateTimeImmutable->add(new DateInterval("P" . $days1 . "D"));56echo "days=" . $days1 . "\n";7echo "end=" . $endDateTimeImmutable->format("Y-m-d") . "\n";outputdays=1 end=2026-06-01
$days ← 30, $start ← DateTimeImmutable, $end ← DateTimeImmutable
1<?php2$days→ 30 = 30;3$start→ DateTimeImmutable = new DateTimeImmutable("2026-05-31", new DateTimeZone("UTC"));4$end→ DateTimeImmutable = $startDateTimeImmutable->add(new DateInterval("P" . $days30 . "D"));56echo "days=" . $days30 . "\n";7echo "end=" . $endDateTimeImmutable->format("Y-m-d") . "\n";outputdays=30 end=2026-06-30