Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

DateInterval

A span of time: days, months, hours.

Syntax

new DateInterval(string $duration)

Parameters

ParameterTypeDescription
$durationstringA 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-15

The 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 days

Notes

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.

Thanks for reading!

Donate to the site — we work hard for you.