PHPackages                             mpd/type-dateinterval - 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. mpd/type-dateinterval

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

mpd/type-dateinterval
=====================

Extended DateInterval class

v1.0.1(1y ago)04LGPL-3.0-onlyPHPPHP &gt;=8.0

Since Dec 31Pushed 1y agoCompare

[ Source](https://github.com/MPDSys/php-type-dateinterval)[ Packagist](https://packagist.org/packages/mpd/type-dateinterval)[ Docs](https://github.com/MPDSys/php-type-dateinterval)[ RSS](/packages/mpd-type-dateinterval/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)DependenciesVersions (3)Used By (0)

type DateInterval
=================

[](#type-dateinterval)

[![Packagist Version](https://camo.githubusercontent.com/0e3189f552905b286fb11f95074afe5c6d3cce0a41c8a932307b274d42884f1b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d70642f747970652d64617465696e74657276616c)](https://packagist.org/packages/mpd/type-dateinterval)[![Packagist Dependency Version](https://camo.githubusercontent.com/b136add8a4c92293f6871d0c81ceddfdce71dfed2f2cb7a87a71151fb1b9c151/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f6d70642f747970652d64617465696e74657276616c2f706870)](https://camo.githubusercontent.com/b136add8a4c92293f6871d0c81ceddfdce71dfed2f2cb7a87a71151fb1b9c151/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f6d70642f747970652d64617465696e74657276616c2f706870)[![GitHub License](https://camo.githubusercontent.com/edb190694d8be9333d0cf3913154768912d4eeca03b4e848a59a073a4a03568f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4d50447379732f7068702d747970652d64617465696e74657276616c)](https://camo.githubusercontent.com/edb190694d8be9333d0cf3913154768912d4eeca03b4e848a59a073a4a03568f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4d50447379732f7068702d747970652d64617465696e74657276616c)

Extension and drop-in replacement of DateInterval class adding ISO 8601 compatibility and comparators

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

[](#installation)

`composer require mpd/type-dateinterval`

Usage
-----

[](#usage)

```
require_once 'vendor/autoload.php';
use mpd\Type\DateInterval\DateInterval;

$i = new DateInterval("P2Y3M1DT8H4M3S");
echo($i->toDuration() . "\n"); //"P2Y3M1DT8H4M3S"

// see https://www.php.net/manual/en/dateinterval.createfromdatestring.php
$i = DateInterval::createFromDateString("+2 days");
echo($i->toDuration() . "\n"); //"P2D"

// see https://www.php.net/manual/en/dateinterval.format.php
echo($i->format("%d days") . "\n"); //"2 days"

$b = new DateInterval("PT48H");
$c = new DateInterval("P2D");
echo($b->isExactlyEqual($i)); // false
echo($c->isExactlyEqual($i)); // true

$date = new DateTime("1970-01-01 00:01:00");
echo($b->cmpRelative($c, $date)); // 0 (equal)
$i = new DateInterval("P2DT1S");
echo($c->cmpRelative($i, $date)); // -1 (c < i)
echo($i->cmpRelative($c, $date)); //  1 (i > c)

$date->add($i);
echo($date->format("Y-m-d H:i:s")); // 1970-01-03 00:01:01
```

```
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 toDuration(): string
    public format(string $format): string
    public cmpRelative(\DateInterval $b, ?DateTimeInterface $baseDate = null): int
    public isExactlyEqual(\DateInterval $b): bool
    public static convertToDurationString(\DateInterval $i): string
    public static createFromDateString(string $datetime): DateInterval|false
    public static compareRelativeToDate(\DateInterval $a, \DateInterval $b, ?DateTimeInterface $base=null): int
    public static areExactlyEqual(\DateInterval $a, \DateInterval $b): bool
}
```

Notes
-----

[](#notes)

This class is based on php's native DateInterval class, can be used as drop in replacement. but also inherits some recommendations/limitations namely "properties should be considered read only".

### isExactlyEqual / areExactlyEqual

[](#isexactlyequal--areexactlyequal)

Compare intervals by components. 24H != 1D, 24H == 24H, 1D == 1D

### cmpRelative / compareRelativeToDate

[](#cmprelative--comparerelativetodate)

Intervals cant be easily compared. For reasons like
1Y == 365,366D; 1M == 28,29,30,31D; leap seconds
But you can compare them exactly from given starting point. You can either specify your own or use implicit current time which gives accurate enough comparison for a lot of cases.
But yes, in some cases this still might not be kind of comparison you need.

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~2 days

Total

2

Last Release

546d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/36997900?v=4)[YuxiUx](/maintainers/YuxiUx)[@YuxiUx](https://github.com/YuxiUx)

---

Top Contributors

[![YuxiUx](https://avatars.githubusercontent.com/u/36997900?v=4)](https://github.com/YuxiUx "YuxiUx (8 commits)")

---

Tags

durationiso8601iso-8601dateinteval

### Embed Badge

![Health badge](/badges/mpd-type-dateinterval/health.svg)

```
[![Health](https://phpackages.com/badges/mpd-type-dateinterval/health.svg)](https://phpackages.com/packages/mpd-type-dateinterval)
```

###  Alternatives

[knplabs/knp-time-bundle

Making your dates and durations look sensible and descriptive

6339.5M56](/packages/knplabs-knp-time-bundle)[brick/date-time

Date and time library

3663.8M105](/packages/brick-date-time)[khill/php-duration

Converts between colon formatted time, human-readable time and seconds

1611.8M21](/packages/khill-php-duration)[sybio/gif-frame-extractor

PHP class that separates all the frames (and their duration) of an animated GIF

180437.0k9](/packages/sybio-gif-frame-extractor)[alroniks/dtms

Classes for manipulate dates and intervals with microseconds precision

187.5k](/packages/alroniks-dtms)

PHPackages © 2026

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