PHP DateTimeImmutable::modify
Advance a calendar date by two days to set a delivery deadline.
A simple example
DateTimeImmutable::modify
- Start with 2024-01-31
- Do this Add one calendar day
- Result 2024-02-01
Syntax
$arrival->modify("+2 days")->format("Y-m-d")
Run these examples in PHP
Save one example at a time as lesson.php. Use PHP 8.2 or later.
php lesson.phpCalculate a delivery date across leap day
A reservation spans the end of February in a leap year. The booking system must use actual calendar days so that the extra day appears in the schedule or stay length.
<?php
$utc = new DateTimeZone("UTC");
$arrival = new DateTimeImmutable("2024-02-28", $utc);
echo $arrival->modify("+2 days")->format("Y-m-d"), PHP_EOL;
2024-03-01
The day after February 28 is February 29 in 2024. One more day brings the date to March 1.
Return a borrowed microscope
A school lends a microscope two days before the end of February in a leap year. Parse the stated local calendar date and add the indicated number of days. Which date goes on the reminder?
Send a year-end reminder
A gallery schedules its reminder one day after the final day of the year. Parse the stated local calendar date and add the indicated number of days. Which date goes on the reminder?
What to watch for
Calendar days include weekends. A business-day promise needs a separate holiday and weekend policy.