PHPackages                             glucnac/datetimeinterval - 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. glucnac/datetimeinterval

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

glucnac/datetimeinterval
========================

DateTimeInterval is wrapper of DateInterval that provides useful methods for calculating delays.

v3.0.0(2y ago)01.1kMITPHPPHP ^8.1

Since Jul 16Pushed 2y ago1 watchersCompare

[ Source](https://github.com/GlucNAc/DateTimeInterval)[ Packagist](https://packagist.org/packages/glucnac/datetimeinterval)[ Docs](https://github.com/GlucNAc/DateTimeInterval)[ RSS](/packages/glucnac-datetimeinterval/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (6)Versions (4)Used By (0)

GlucNAc/DateTimeInterval
========================

[](#glucnacdatetimeinterval)

[![Source Code](https://camo.githubusercontent.com/05a6a4a51d09b774f053a0b779ccf49988b41f2fe8b5cce3262d3b18a3005ca3/687474703a2f2f696d672e736869656c64732e696f2f62616467652f736f757263652d476c75634e41632f4461746554696d65496e74657276616c2d626c75652e7376673f7374796c653d666c61742d737175617265)](https://github.com/GlucNAc/DateTimeInterval)[![Latest Version](https://camo.githubusercontent.com/0984d3bd1cf457627d9ab743bdd63c9e6155b187e5115e3bc288de8a83dca4ab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f476c75634e41632f4461746554696d65496e74657276616c2e7376673f7374796c653d666c61742d737175617265266c6162656c3d72656c65617365)](https://packagist.org/packages/GlucNAc/DateTimeInterval)[![PHP Version](https://camo.githubusercontent.com/1b86c3316b0012a9a50125ea9095d6a129b5f9a3648a280260f4865c3871e1d5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f476c75634e41632f4461746554696d65496e74657276616c2e7376673f7374796c653d666c61742d737175617265)](https://php.net)[![Pipeline status](https://camo.githubusercontent.com/d59abbb79ead7c180b7e0fae5c5329aa9ce98cb84b929ea4f2d4356eb25d8468/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f476c75634e41632f4461746554696d65496e74657276616c2f636f6e74696e756f75732d696e746567726174696f6e2e796d6c3f6272616e63683d6d6173746572267374796c653d666c61742d737175617265266c6f676f3d676974687562)](https://github.com/GlucNAc/DateTimeInterval/actions/workflows/continuous-integration.yml)[![Coverage Status](https://camo.githubusercontent.com/29cd2d038a8506c8835f941457fdc55305da5e0ca68834cb49a9c42b46b98e4e/68747470733a2f2f636f6465636f762e696f2f67682f476c75634e41632f4461746554696d65496e74657276616c2f67726170682f62616467652e7376673f746f6b656e3d31424a59345434483944)](https://codecov.io/gh/GlucNAc/DateTimeInterval)[![Total Downloads](https://camo.githubusercontent.com/6e77d2e1414597aa24958e9f4e74dbdf9f7b36be71f418eb7beeb990e69498b0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f476c75634e41632f4461746554696d65496e74657276616c2e7376673f7374796c653d666c61742d73717561726526636f6c6f72423d6d656469756d76696f6c6574726564)](https://packagist.org/packages/GlucNAc/DateTimeInterval)

When you want to compute a delay between two DateTimes, it can be quickly tricky to get the result you are expecting for, and sometimes you need to perform some calculations, which can lead to incorrect result.

DateTimeInterval is wrapper of DateInterval that provides useful methods for calculating delays. Thanks to this library, there is no need to compute results returned from DateInterval anymore. Moreover, all of its methods are fully tested, and are explicitly named and commented.

In that way, you don't have to copy/paste the logic you implemented on another project, just install this library and enjoy.

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

[](#installation)

The preferred method of installation is via [Composer](http://getcomposer.org/). Run the following command to install the package and add it as a requirement to your project's `composer.json`:

```
composer require glucnac/datetimeinterval
```

Usage
-----

[](#usage)

```
$firstDate = new DateTimeImmutable('2022-07-15');
$secondDate = new DateTimeImmutable('2022-08-15');
$dateTimeInterval = new DateTimeInterval($firstDate, $secondDate);

// Absolute count of days
echo $dateTimeInterval->getDays(); // 31

// Relative count of days
echo $dateTimeInterval->getDays(false); // 0 (15th day of the month (august) - 15th day of the month (july))
```

```
$firstDate = new DateTimeImmutable('2022-07-15');
$secondDate = new DateTimeImmutable('2023-08-15');
$dateTimeInterval = new DateTimeInterval($firstDate, $secondDate);

// Absolute count of months
echo $dateTimeInterval->getMonths(); // 13

// Relative count of months
echo $dateTimeInterval->getMonths(false); // 1 (8th month of the year (august) - 7th month of the year (july))
```

And so on :

```
$dateTimeInterval->getYears();
$dateTimeInterval->getHours();
$dateTimeInterval->getMinutes();
$dateTimeInterval->getSeconds();
$dateTimeInterval->getMicroseconds();
```

Result can be signed or unsigned, depending on the third parameter of the constructor (`$returnAbsoluteValue`, default is true).

```
$firstDate = new DateTimeImmutable('2022-07-15');
$secondDate = new DateTimeImmutable('2022-08-15');
$dateTimeInterval = new DateTimeInterval($firstDate, $secondDate, false);

echo $dateTimeInterval->getDays(); // -31
```

Result can be formatted as a string :

```
$supDate = new DateTime('2021-08-27 00:00:00');
$infDate = new DateTime('2022-09-29 23:35:59');
$dateTimeInterval = new DateTimeInterval($firstDate, $secondDate);

// Internally uses DateInterval::format() method
echo $dateTimeInterval->format('%y years, %m months, %d days, %h hours, %i minutes, %s seconds'); // 1 years, 1 months, 2 days, 23 hours, 35 minutes, 59 seconds
```

Nostalgic about DateInterval ? No problem, you can get it back :

```
$dateInterval = $dateTimeInterval->getDateInterval();
```

What is the purpose ?
---------------------

[](#what-is-the-purpose-)

To understand the usefulness of this library, let's try to compute some delays.

### Get delays using date\_diff()

[](#get-delays-using-date_diff)

```
// Let's say you want to compute some delays between 2022-07-15 and 2022-08-15.

$firstDate = new DateTimeImmutable('2022-07-15');
$secondDate = new DateTimeImmutable('2022-08-15');

$dateInterval = date_diff($secondDate, $firstDate);
// OR
$dateInterval = $firstDate->diff($secondDate);

// You want to count the days between these two dates. Using DateInterval,
// you notice that two properties are available :

// PhpDoc states: "Totals number of days in the interval span"
$dateInterval->days;

// PhpDoc states: "Number of days"
$dateInterval->d;

// Well, the doc of ->d is not really clear, let's see the difference between these two returns

echo $dateInterval->days; // 30
echo $dateInterval->d; // 0

// Nice, it seems ->d is a relative count, whereas ->days give an absolute count

// What about getting the number of months then ?

// PhpDoc states: "Number of months"
echo $dateInterval->m // 1

// We don't yet if it's a relative or an absolute count. So let's change the year to next one :
$firstDate = new DateTimeImmutable('2022-07-15');
$secondDate = new DateTimeImmutable('2023-08-15');
$dateInterval = date_diff($secondDate, $firstDate);

echo $dateInterval->m // 1

// Now it's clear: ->m also returns a relative count
```

Actually: `->y`, `->m`, `->d`, `->h`, `->i`, `->s`, `->f`: all are returning a relative count, which means that for any absolute result, you must perform some calculations using `->days`, which is the only result to be absolute. Even though these calculations are simple, rewriting them on each of your project is boring and can lead to inattention mistakes.

That's why this library has been made for: to abstract the calculations and to get a more object-oriented coding.

Copyright and License
---------------------

[](#copyright-and-license)

The GlucNAc/DateTimeInterval library is copyright © [GlucNAc](https://gitlab.com/GlucNAc) and licensed for use under the MIT License (MIT). Please see [LICENSE](https://gitlab.com/GlucNAc/DateTimeInterval/-/blob/master/LICENSE) for more information.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

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 ~336 days

Total

3

Last Release

731d ago

Major Versions

v2.0.1 → v3.0.02024-05-17

PHP version history (3 changes)v2.0.0PHP ^7.4

v2.0.1PHP ^7.4|^8.1

v3.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/dd3b97d61b85c80668786bd955e5eb38f972f6f3a371e9a87fccce3f3244d394?d=identicon)[GlucNAc](/maintainers/GlucNAc)

---

Top Contributors

[![GlucNAc](https://avatars.githubusercontent.com/u/51360240?v=4)](https://github.com/GlucNAc "GlucNAc (16 commits)")

---

Tags

datetimetimedateintervalDateTime operationdate\_diff

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleECS

Type Coverage Yes

### Embed Badge

![Health badge](/badges/glucnac-datetimeinterval/health.svg)

```
[![Health](https://phpackages.com/badges/glucnac-datetimeinterval/health.svg)](https://phpackages.com/packages/glucnac-datetimeinterval)
```

###  Alternatives

[league/period

Time range API for PHP

7335.4M21](/packages/league-period)[brick/date-time

Date and time library

3623.3M61](/packages/brick-date-time)[aeon-php/calendar

PHP type safe, immutable calendar library

2079.7M16](/packages/aeon-php-calendar)[tplaner/when

Date/Calendar recursion library.

5261.0M5](/packages/tplaner-when)[kartik-v/php-date-formatter

A Javascript datetime formatting and manipulation library using PHP date-time formats.

461.5M3](/packages/kartik-v-php-date-formatter)[dater/dater

Compact PHP library for working with date/time in different formats &amp; timezones.

14282.3k](/packages/dater-dater)

PHPackages © 2026

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