PHP 8.0.26 Released!

The DateInterval class

(PHP 5 >= 5.3.0, PHP 7, PHP 8)


Represents a date interval.

A date interval stores either a fixed amount of time (in years, months, days, hours etc) or a relative time string in the format that DateTimeImmutable's and DateTime's constructors support.

More specifically, the information in an object of the DateInterval class is an instruction to get from one date/time to another date/time. This process is not always reversible.

A common way to create a DateInterval object is by calculating the difference between two date/time objects through DateTimeInterface::diff().

Since there is no well defined way to compare date intervals, DateInterval instances are incomparable.

Class synopsis

class DateInterval {
/* Properties */
public int $y;
public int $m;
public int $d;
public int $h;
public int $i;
public int $s;
public float $f;
public int $invert;
public mixed $days;
public bool $from_string;
public string $date_string;
/* Methods */
public __construct(string $duration)
public static createFromDateString(string $datetime): DateInterval|false
public format(string $format): string



The available properties listed below depend on PHP version, and should be considered as readonly.


Number of years.


Number of months.


Number of days.


Number of hours.


Number of minutes.


Number of seconds.


Number of microseconds, as a fraction of a second.


Is 1 if the interval represents a negative time period and 0 otherwise. See DateInterval::format().


If the DateInterval object was created by DateTimeImmutable::diff() or DateTime::diff(), then this is the total number of full days between the start and end dates. Otherwise, days will be false.


If the DateInterval object was created by DateInterval::createFromDateString(), then this property's value will be true, and the date_string property will be populated. Otherwise, the value will be false, and the y to f, invert, and days properties will be populated.


The string used as argument to DateInterval::createFromDateString().


Version Description
8.2.0 The from_string and date_string properties were added for DateInterval instances that were created using the DateInterval::createFromDateString() method.
8.2.0 Only the y to f, invert, and days will be visible.
7.4.0 DateInterval instances are incomparable now; previously, all DateInterval instances were considered equal.
7.1.0 The f property was added.

Table of Contents

add a note

User Contributed Notes 1 note

nateb at gurutechnologies dot net
2 years ago
Many people have commented on doing a reverse interval on a date time.  I personally find a backwards year to be a little strange to think about and instead opt to work with just intervals.  This is the easiest I have found.

= new DateInterval('P1Y');
$one_year_ago = new DateTime();

Instead of:

= new DateInterval( "P1Y" );
$one_year_ago->invert = 1;
$one_year_ago = new DateTime();
To Top