DateInterval
A span of time: days, months, hours.
Syntax
new DateInterval(string $duration)Parameters
| Parameter | Type | Description |
|---|---|---|
| $duration | string | A notation like `P1Y2M3D` or `PT4H30M`. |
Return value
DateIntervalAn object with the `y`, `m`, `d`, `h`, `i`, `s` and `days` properties.
Examples
Adding two weeks:
<?php
$date = new DateTimeImmutable('2026-03-01');
echo $date->add(new DateInterval('P2W'))->format('Y-m-d');Output:
2026-03-15The difference between dates:
<?php
$from = new DateTimeImmutable('2026-01-01');
$to = new DateTimeImmutable('2026-03-15');
$diff = $from->diff($to);
echo $diff->m, ' months ', $diff->d, ' days, total ', $diff->days, ' days';Output:
2 months 14 days, total 73 daysNotes
The notation starts with P, and T separates the time: P1D is a day, PT1H an hour, P1M a month, PT1M a minute.
See also
- DateTimeImmutable — A date and time object that cannot be changed: every operation returns a new object.
- date_diff — Calculates the difference between two dates.
- DatePeriod — A set of dates with an equal step to loop over.
In the tutorial
- Date and time — Working with dates in PHP — the date function, the DateTimeImmutable class, formats, adding to dates and the difference between them.