PHPackages                             kirouane/interval - 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. kirouane/interval

ActiveLibrary

kirouane/interval
=================

Library to handel intervals

1.3.3(5y ago)2875.3k↑12.5%7[1 issues](https://github.com/Kirouane/interval/issues)MITPHPPHP &gt;= 7.1

Since Nov 21Pushed 5y ago4 watchersCompare

[ Source](https://github.com/Kirouane/interval)[ Packagist](https://packagist.org/packages/kirouane/interval)[ Docs](https://github.com/Kirouane/interval)[ RSS](/packages/kirouane-interval/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (12)Versions (13)Used By (0)

[![Travis](https://camo.githubusercontent.com/08846d29faf8e33ee249cdd231cb1e76b66610a94234af4e679415b629de0195/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f4b69726f75616e652f696e74657276616c2f6d61737465722e737667)](http://travis-ci.org/Kirouane/interval)[![Coverage Status](https://camo.githubusercontent.com/8f350e9958797bcac44fd06fda4adb803e3e9e6820eb813f0f376d9ac3476151/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f4b69726f75616e652f696e74657276616c2f62616467652e737667)](https://coveralls.io/github/Kirouane/interval?branch=develop)[![Codacy Badge](https://camo.githubusercontent.com/f942f7f140d07162a0a3d15ac86296307f12fa9a85332ca6660177bd227a5ce7/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3738336331383633376535373438393462633661333765316335633735653933)](https://www.codacy.com/app/Kirouane/interval?utm_source=github.com&utm_medium=referral&utm_content=Kirouane/interval&utm_campaign=Badge_Grade)[![Total Downloads](https://camo.githubusercontent.com/31f606a93570f7a37354039469f67e426d6a549d45ff871d23efd3d8077e5215/68747470733a2f2f706f7365722e707567782e6f72672f6b69726f75616e652f696e74657276616c2f646f776e6c6f616473)](https://packagist.org/packages/kirouane/interval)[![Latest Stable Version](https://camo.githubusercontent.com/eaaffc65cbda9097342b27dcd820f72a3a383e46aa9d42ac3e6718b374577121/68747470733a2f2f706f7365722e707567782e6f72672f6b69726f75616e652f696e74657276616c2f762f737461626c65)](https://packagist.org/packages/kirouane/interval)

Interval
========

[](#interval)

This library provides some tools to handle intervals. For instance, you can compute the union or intersection of two intervals.

Use cases
---------

[](#use-cases)

- Availabilities calculation.
- Scheduling/calendar/planning.
- Mathematics interval computation with open/closed boundaries
- etc

Features
--------

[](#features)

- It computes some operations between two **intervals**: union, intersection and exclusion.
- It computes some operations between two **sets of intervals**: exclusion for now.
- It handles several types of boundaries : float, **\\DateTime** and integer.
- It handles **infinity** type as boundary.
- Ability to **combine** infinity with \\DateTime and other types.
- filter, sort, map.
- Immutability.
- Chain operations.

Quality
-------

[](#quality)

- Code coverage [![Coverage Status](https://camo.githubusercontent.com/8f350e9958797bcac44fd06fda4adb803e3e9e6820eb813f0f376d9ac3476151/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f4b69726f75616e652f696e74657276616c2f62616467652e737667)](https://coveralls.io/github/Kirouane/interval?branch=develop)
- Mutation test : Code coverage more than **90%**
- Takes care of **performance** and **memory usage**
- PSR1/PSR2, Code Smell

Install
-------

[](#install)

`composer require kirouane/interval`

Basic usage
-----------

[](#basic-usage)

Let's assume an interval \[20, 40\]. We instantiate a new Interval object .

```
$interval = new Interval(20, 40);// [20, 40];
```

or

```
$interval = Interval::create('[20,40]');// [20, 40];
```

We can do some operations like :

- Intersection :

```
echo $interval->intersect(new Interval(30, 60)); // [30, 40];
```

- Union :

```
echo $interval->union(new Interval(30, 60)); // {[20, 60]};
```

or

```
echo $interval->union(new Interval(60, 100)); // {[20, 40], [60, 100]};
```

- Exclusion :

```
echo $interval->exclude(new Interval(30, 60)); // {[20, 30[};
```

or

```
echo $interval->exclude(new Interval(30, 35)); // {[20, 30[, ]35, 40]};
```

We can compare two intervals as well:

- Overlapping test :

```
echo $interval->overlaps(new Interval(30, 60)); // true;
```

- Inclusion test :

```
echo $interval->includes(new Interval(30, 60)); // false;
```

Use DateTimeInterface as boundary
---------------------------------

[](#use-datetimeinterface-as-boundary)

```
$interval = new Interval(new \DateTime('2016-01-01'), new \DateTime('2016-01-10'));
// [2016-01-01T00:00:00+01:00, 2016-01-10T00:00:00+01:00];
```

- Union :

```
echo $interval->union(Interval::create('[2016-01-10, 2016-01-15]'));
// {[2016-01-01T00:00:00+01:00, 2016-01-15T00:00:00+01:00]};
```

Use Infinity as boundary
------------------------

[](#use-infinity-as-boundary)

```
$interval = new Interval(-INF, INF);// ]-∞, +∞[;
```

- Exclusion :

```
echo $interval->exclude(Interval::create('[2016-01-10, 2016-01-15]'));
// {]-∞, 2016-01-10T00:00:00+01:00[, ]2016-01-15T00:00:00+01:00, +∞[};
```

Operations on sets (arrays) of intervals
----------------------------------------

[](#operations-on-sets-arrays-of-intervals)

```
$intervals = Intervals::create(['[0,5]', '[8,12]']);// {[0, 5], [8, 12]};
```

- Exclusion :

```
echo $intervals->exclude(Intervals::create(['[3,10]'])); // {[0, 3[, ]10, 12]};
```

Chaining
--------

[](#chaining)

```
$result = Interval
    ::create('[10, 20]')
    ->intersect(new Interval(11, 30))
    ->union(new Interval(15, INF))
    ->exclude(Intervals::create(['[18, 20]', '[25, 30]', '[32, 35]', '[12, 13]']))
    ->sort(function (Interval $first, Interval $second) {
        return $first->getStart()->getValue()  $second->getStart()->getValue();
    })
    ->map(function (Interval $interval) {
        return new Interval(
            $interval->getStart()->getValue() ** 2,
            $interval->getEnd()->getValue() ** 2
        );
    })
    ->filter(function (Interval $interval) {
        return $interval->getEnd()->getValue() > 170;
    });

// {[169, 324], [400, 625], [900, 1024], [1225, +∞[};

echo $result;
```

Advanced usage
--------------

[](#advanced-usage)

You can create intervals with **open** boundaries :

```
$result = Intervals
    ::create([']10, +INF['])
    ->exclude(Intervals::create([']18, 20]', ']25, 30[', '[32, 35]', ']12, 13]']));

// {]10, 12], ]13, 18], ]20, 25], [30, 32[, ]35, +∞[}
```

Contributing
------------

[](#contributing)

You are very welcomed to contribute to this Library!

- Clone `git clone https://github.com/Kirouane/interval.git`
- Install `composer install`or `make install` (with docker and docker-compose)
- Test `vendor/bin/phpunit`
- Build `vendor/bin/grumphp run`

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity41

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 70.2% 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 ~141 days

Recently: every ~245 days

Total

8

Last Release

2111d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3df43747132f114d241a3edbe7f4f89174b19b0ad853a29bd7789cdb328b2e62?d=identicon)[Kirouane](/maintainers/Kirouane)

---

Top Contributors

[![Kirouane](https://avatars.githubusercontent.com/u/16067161?v=4)](https://github.com/Kirouane "Kirouane (66 commits)")[![nassimkirouane](https://avatars.githubusercontent.com/u/10706013?v=4)](https://github.com/nassimkirouane "nassimkirouane (25 commits)")[![adacio](https://avatars.githubusercontent.com/u/1171981?v=4)](https://github.com/adacio "adacio (1 commits)")[![andreybolonin](https://avatars.githubusercontent.com/u/2576509?v=4)](https://github.com/andreybolonin "andreybolonin (1 commits)")[![maks-rafalko](https://avatars.githubusercontent.com/u/3725595?v=4)](https://github.com/maks-rafalko "maks-rafalko (1 commits)")

---

Tags

datetimeexclusionsinfinityintersectionintervalmanipulate-intervalsperiodphpphp7planningschedulingunionphptimeintervalperiodplanningalgebrabookingexclusioninfinity

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/kirouane-interval/health.svg)

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

###  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)[dater/dater

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

14282.3k](/packages/dater-dater)[zjkal/time-helper

一个简单快捷的PHP日期时间助手类库。 a smart PHP datetime helper library.

21128.6k1](/packages/zjkal-time-helper)[mpratt/relativetime

A library that calculates the time difference between two dates and returns the result in words (Example: 5 minutes ago or 5 Minutes left). The library supports other languages aswell like Spanish and German.

33115.1k2](/packages/mpratt-relativetime)[advmaker/carbon-period

Extension of nesbot/carbon plugin, to work with date period

10116.0k](/packages/advmaker-carbon-period)

PHPackages © 2026

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