What it is and why to use it
A reminder may be due three calendar days after a start date. DateInterval represents an interval that can be added to or subtracted from a date. DateTimeImmutable::add() returns a new date instead of changing the original. This gives a clear way to retain both the original event and a calculated due date.
How it works
In an interval string, P introduces the period: P3D means three days and P1M means one month. T separates time components, so PT30M means thirty minutes. DateTimeImmutable::diff() computes a DateInterval describing a transition to another date. Calendar arithmetic observes time-zone rules; it is not always equivalent to adding a fixed count of elapsed seconds.
Common mistakes and limitations
Do not confuse P1M with PT1M. Adding one month at the end of a long month can overflow into the following month, so billing dates need an explicit policy. Do not assume a calendar day always contains 86400 elapsed seconds across daylight-saving changes. Always keep the immutable method’s return value.
Run the examples locally
Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.
The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.
These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.
Check your interpreter, lint and run one saved example
php -v
php -l example.php
php example.phpContinue learning
Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.
Syntax
$due = $start->add(new DateInterval('P3D'));Examples
Calculate a due date without changing the start
Add three calendar days and compare the result with the original.
<?php
declare(strict_types=1);
$start = new DateTimeImmutable('2026-10-10', new DateTimeZone('UTC'));
$due = $start->add(new DateInterval('P3D'));
echo $start->format('Y-m-d'), PHP_EOL;
echo $due->format('Y-m-d'), PHP_EOL;
echo $start->diff($due)->format('%a days'), PHP_EOL;
- The UTC zone makes this small example independent of the machine’s local settings.
- P3D describes three days; add returns a new object stored in due.
- The original date still prints October 10. diff computes the interval and %a formats the total number of days for this comparison.
- Expected output: 2026-10-10 2026-10-13 3 days
Distinguish a month from minutes
Use the position of T to choose the interval unit.
<?php
declare(strict_types=1);
$start = new DateTimeImmutable('2026-04-10 09:00:00', new DateTimeZone('UTC'));
echo $start->add(new DateInterval('P1M'))->format('Y-m-d H:i'), PHP_EOL;
echo $start->add(new DateInterval('PT30M'))->format('Y-m-d H:i'), PHP_EOL;
- P1M places M in the date part and means one calendar month.
- PT30M places M after T and means thirty minutes.
- The fixed day 10 avoids end-of-month overflow so the unit difference is easy to see. Real recurring schedules need boundary tests.
- Expected output: 2026-05-10 09:00 2026-04-10 09:30
Best practices
- Specify whether a requirement means calendar time or elapsed seconds.
- Test month boundaries and daylight-saving transitions.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual