Compare two DateInterval-Objects with one another

1.1.0 2020-01-14 09:59 UTC

This package is auto-updated.

Last update: 2020-07-14 11:27:47 UTC


Compare two DateInterval-Objects with one another.

Build Status Code Climate Coverage Status Scrutinizer Code Quality

Total Downloads Latest Stable Version License


DateIntervalComparator is best installed using composer

composer require org_heigl/dateintervalcomparator


$comparator = new Org_Heigl\DateIntervalComparator\DateIntervalComparator()
echo $comparator->compare(new Dateinterval('P1Y'), new DateInterval('P1M'));
// 1

The DateIntervalComparator uses the same return values all comparison-functions in PHP use. When the first value is smaller than the second it returns -1, if the first is greater than the second it returns 1 and when both are equals it returns 0. The method can therefore be used as callback to sort arrays.

This library assumes that the compared intervals are sensible and alike. Comparing 3 months and 4 days against 5 months and 7 days is what this library is made for. It's not been made for comparing 3 months and 4 days against 94 days. Due to the different number of days in a month this comparison will be different depending on the start-date and we do want to compare only the Intervals and not the resulting end-dates!

As long as the values do not exceed their moduli (e.g. a value of 13 for month or 25 for the hour) you are on the safe side.

You can even enforce this safe side by setting php $comparator->safe(true). That will then throw an exception when one of the values for month, day, hour, minute or second will exceed the values 12, 31, 24, 60 and 60 resp.


Take care! DateIntervals can be nasty!

Due to their nature DateIntervals do not take Date or Time into account! So DateInterval('P1M') might or might not be the same as DateInterval('P30D')! This Library will compare on a "same entity"-level. So first years are compared, then months, then weeks and so on. When you have an interval that is describing only 55 weeks and compare that to one that is only describing one year, the one year will be considered greater even though 55 weeks are more. So you should only compare comparable intervals!

The same can happen with f.e. new DateInterval('PT36H') and new DateInterval('P1DT12H') You'd think they are the same, but consider DaylightSavingsTime and they suddenly might not be the same! So currently the P1DT12H would be considered the "higher" one.


This library is published under the MIT-License. Find a copy in the