PHPackages                             org\_heigl/dateintervalcomparator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. org\_heigl/dateintervalcomparator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

org\_heigl/dateintervalcomparator
=================================

Compare two DateInterval-Objects with one another

1.1.0(6y ago)887.5k—0%4[1 issues](https://github.com/heiglandreas/DateIntervalComparator/issues)2MITPHPPHP ^7.1CI failing

Since Oct 11Pushed 5y ago1 watchersCompare

[ Source](https://github.com/heiglandreas/DateIntervalComparator)[ Packagist](https://packagist.org/packages/org_heigl/dateintervalcomparator)[ RSS](/packages/org-heigl-dateintervalcomparator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (6)Used By (2)

DateIntervalComparator
======================

[](#dateintervalcomparator)

Compare two DateInterval-Objects with one another.

[![Build Status](https://camo.githubusercontent.com/dfcea578d85d92098175d8c708c83772fad771ef14911a6cf3db723e9fcdc28e/68747470733a2f2f7472617669732d63692e6f72672f686569676c616e64726561732f44617465496e74657276616c436f6d70617261746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/heiglandreas/DateIntervalComparator)[![Code Climate](https://camo.githubusercontent.com/d1d7f1b6e39cbf605583c2bbddabb528df3766c0ce4c0f31089c15bf3a2c40b5/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f686569676c616e64726561732f44617465496e74657276616c436f6d70617261746f722f6261646765732f6770612e737667)](https://codeclimate.com/github/heiglandreas/DateIntervalComparator)[![Coverage Status](https://camo.githubusercontent.com/94ec3da319e05773df4ae5a5f922b1cf5357e2d686fbc6d186c5da8e5220b0dd/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f686569676c616e64726561732f44617465496e74657276616c436f6d70617261746f722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/heiglandreas/DateIntervalComparator?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/62779e87511b02bce135c7fc5676d01ea02f8a2048248a9f064c2e376acda0e2/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f686569676c616e64726561732f44617465496e74657276616c436f6d70617261746f722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/heiglandreas/DateIntervalComparator/?branch=master)

[![Total Downloads](https://camo.githubusercontent.com/4ed76d0ac99fde94f0fdf5f7237976cf6bb89ce37840eea3645e9b24bdf711d9/68747470733a2f2f706f7365722e707567782e6f72672f6f72675f686569676c2f64617465696e74657276616c636f6d70617261746f722f646f776e6c6f616473)](https://packagist.org/packages/org_heigl/dateintervalcomparator)[![Latest Stable Version](https://camo.githubusercontent.com/056283cd8f144464ea489daa9e734dddeaf20c7d69f806c8d784694013ef7cdf/68747470733a2f2f706f7365722e707567782e6f72672f6f72675f686569676c2f64617465696e74657276616c636f6d70617261746f722f762f737461626c65)](https://packagist.org/packages/org_heigl/dateintervalcomparator)[![License](https://camo.githubusercontent.com/2eeda269c20aa03b24ace90cc45344b2df1b2d6ca1ffd26ed38242915d30c40e/68747470733a2f2f706f7365722e707567782e6f72672f6f72675f686569676c2f64617465696e74657276616c636f6d70617261746f722f6c6963656e7365)](https://packagist.org/packages/org_heigl/dateintervalcomparator)

Installation
------------

[](#installation)

DateIntervalComparator is best installed using [composer](https://getcomposer.org)

```
composer require org_heigl/dateintervalcomparator

```

Usage
-----

[](#usage)

```
$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.

Caveat/Limitations
------------------

[](#caveatlimitations)

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.

License
-------

[](#license)

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

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 87.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~595 days

Total

3

Last Release

2316d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5ae5183aaad2bc7453230704bd6991dc6ccbcd6e775c6a29efdc94350a69f247?d=identicon)[heiglandreas](/maintainers/heiglandreas)

---

Top Contributors

[![heiglandreas](https://avatars.githubusercontent.com/u/91998?v=4)](https://github.com/heiglandreas "heiglandreas (14 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/org-heigl-dateintervalcomparator/health.svg)

```
[![Health](https://phpackages.com/badges/org-heigl-dateintervalcomparator/health.svg)](https://phpackages.com/packages/org-heigl-dateintervalcomparator)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
