Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Date and Time

The DateInterval class

Reference for the PHP DateInterval class: ISO 8601 duration strings like P1M2DT3H, properties, format(), createFromDateString and using intervals with add() and sub().

DateInterval represents a length of time (not a moment): "2 days", "1 month and 15 minutes", "3 years". You add an interval to a date with add(), subtract it with sub(), and get one back from diff().

Intervals are created from ISO 8601 duration strings. They start with P; date parts follow (Y years, M months, W weeks, D days), then a T and the time parts (H hours, M minutes, S seconds). So P1Y2M10DT2H30M is 1 year, 2 months, 10 days, 2 hours and 30 minutes. Note that M before the T means months and after it means minutes.

PHP
<?php
$i = new DateInterval("P1DT12H");
echo $i->d, " ", $i->h;   // 1 12

Try it

PHP Building and applying intervals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP From diff(), from text, and weeks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
new DateInterval(string $duration)

DateInterval::createFromDateString(string $datetime): DateInterval|false
$interval->format(string $format): string
Name Description
$duration An ISO 8601 duration such as P1D, PT36H or P1Y2M10DT2H30M.
$datetime Relative text such as 1 day, 2 weeks ago or last day of next month.
$format Characters beginning with %: %y years, %m months, %d days, %h hours, %i minutes, %s seconds (add upper case or %Y %M %D %H %I %S for zero-padded), %a total days (from diff()), %R sign (+ or -), %r sign (- only when negative), %f microseconds, %% percent sign.

Public properties: y, m, d, h, i, s, f (microseconds as a fraction of a second), invert (1 when the interval is negative) and days (the total number of days when the object came from diff(), otherwise false).

Exceptions: the constructor throws DateMalformedIntervalStringException (PHP 8.3 and later; Exception in earlier versions) for strings that are not valid ISO 8601 durations.

Usage notes

  • M means two things. Months before T, minutes after it. PT5M is five minutes, P5M five months.
  • Intervals are not normalized. PT90M keeps i = 90; it does not turn into 1 hour 30 minutes. If you need hours and minutes, compute them yourself.
  • Calendar-relative. P1M has no fixed length. Its effect depends on the date it is added to: 28 to 31 days.
  • diff() results have days. Use $interval->days for the total number of days, and $interval->d only for the leftover days after months.
  • Negative intervals: invert is 1 when the second date is earlier. add() of an inverted interval goes backwards.
  • No fractional parts in ISO strings: P1.5D is invalid. Use PT36H.
  • Daylight saving. Time-of-day parts add elapsed time, day parts keep the local clock time.
  • createFromDateString() keeps only simple offsets. Phrases such as last day of next month or next monday are not stored in the interval; use modify() for those.