PHPackages                             max13/conditional-period - 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. max13/conditional-period

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

max13/conditional-period
========================

ConditionalPeriod is a php class allowing to store 2 numerical/DateInterval intervals resulting in a DateInterval.

v0.9.0(3y ago)015MITPHPPHP ^8.0

Since Feb 27Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Max13/conditional-period-php)[ Packagist](https://packagist.org/packages/max13/conditional-period)[ Docs](https://github.com/max13/conditional-period-php)[ RSS](/packages/max13-conditional-period/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (12)Used By (0)

ConditionalPeriod
=================

[](#conditionalperiod)

`ConditionalPeriod` is a php class allowing to store 2 numerical/`DateInterval` intervals resulting in a `DateInterval`.

It is, actually, a requirement for a project I'm working on. Typically, it concerns the working contracts. Here is an example using integers as conditions:

- If the **category** of the employee is **between 1 and 5**, the *prior notice* is **1 month**.
- If the **category** of the employee is **between 6 and 7**, the *prior notice* is **2 months**.
- If the **category** of the employee is **between 8 and 12**, the *prior notice* is **3 months**.

Here is another example using date intervals as conditions:

- If the **fixed term contract** lasts from 0 to **6 months**, then the *trial period* is **15 days**.
- Above **6 months**, the *trial period* is **1 month**.

Expressing these interval with conditions using `ConditionalPeriod` is super duper easy and can be expressed in 2 ways: Using 4 arguments on the constructor, or using a string syntax.

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

[](#installation)

You can use composer: `composer require max13/conditional-period:^1.0`

Or download the repo and add the files (in `/src`) to your project.

Usage
-----

[](#usage)

### Using 4 arguments on the constructor, aka "classic instantiation"

[](#using-4-arguments-on-the-constructor-aka-classic-instantiation)

```
use Carbon\CarbonInterval;
use MX\ConditionalCollection;
use MX\ConditionalPeriod;
...
$prior_notices = [
    new ConditionalPeriod(
        ConditionalType::CATEGORY,
        1,
        5,
        CarbonInterval::make('1 month')
    ),
    new ConditionalPeriod(
        ConditionalType::CATEGORY,
        6,
        7,
        CarbonInterval::make('2 months')
    ),
    new ConditionalPeriod(
        ConditionalType::CATEGORY,
        8,
        12,
        CarbonInterval::make('3 months')
    ),
];

$prior_notices = ConditionalCollection::fromArray($prior_notices);

$trial_periods = [
    new ConditionalPeriod(
        ConditionalType::DURATION,
        CarbonInterval::make(0),
        CarbonInterval::make('6 months'),
        CarbonInterval::make('15 days')
    ),
    new ConditionalPeriod(
        ConditionalType::DURATION,
        CarbonInterval::make('6 months'),
        CarbonInterval::make('99 years'), // Equivalent to +∞
        CarbonInterval::make('1 month')
    ),
];

$trial_periods = ConditionalCollection::fromArray($trial_periods);
```

### Using the short string format, aka "badass mode"

[](#using-the-short-string-format-aka-badass-mode)

```
use MX\ConditionalCollection;
use MX\ConditionalPeriod;
...
$prior_notices = [
    new ConditionalPeriod('C1-5P1M'),
    new ConditionalPeriod('C6-7P2M'),
    new ConditionalPeriod('C8-12P3M'),
];

// OR
$prior_notices = new ConditionalCollection;
$prior_notices[] = new ConditionalPeriod('C1-5P1M');
$prior_notices[] = 'C6-7P2M';
$prior_notices[] = new ConditionalPeriod('C8-12P3M');

$trial_periods = [
    new ConditionalPeriod('DP0DP6MP15D'),
    new ConditionalPeriod('DP6MP99YP1M')
];

// OR
$trial_periods = ConditionalCollection::parse('DP0DP6MP15D,DP6MP99YP1M');
```

### Miscellaneous

[](#miscellaneous)

You may of may not have noticed it, but every `CarbonInterval` argument can be replaced by either its [`ISO8601` duration spec](https://en.wikipedia.org/wiki/ISO_8601#Durations) (the same way you can [instanciate a `DateInterval`](http://php.net/dateinterval.construct), or a relative date string (as you can with `CarbonInterval`.

So, here are the 5 same ways to input a `Carboninterval` using `ConditionalPeriod` constructor:

- `Carboninterval::createFromDateString('1 year, 2 months, 3 days')`
- `Carboninterval::make('P1Y2M3D');`
- `new Carboninterval('P1Y2M3D');`
- `'1 year, 2 months, 3 days'`
- `'P1Y2M3D'`

And there are 5 ways to create a `ConditionalCollection`:

- `ConditionalCollection::create(new ConditionalPeriod(…))`, which instanciates it and sets its first value (); // Or ConditionalPeriod string form
- `new ConditionalCollection`, then push to it: `c->push(new ConditionalPeriod(…)); // or ConditionalPeriod string form`
- `new ConditionalCollection`, then treat it as an array: `$c[] = new ConditionalPeriod(…); // or ConditionalPeriod string form`
- `ConditionalCollection::parse(…)`, which takes its own `toString()` form
- `ConditionalCollection::fromArray(…)`, which takes an array of `ConditionalPeriod` or its string form

Need help?
----------

[](#need-help)

Open an issue.

Now have fun.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity62

Established project with proven stability

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

Recently: every ~313 days

Total

11

Last Release

1372d ago

PHP version history (2 changes)v0.4.0PHP &gt;=5.6

v0.9.0PHP ^8.0

### Community

Maintainers

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

---

Top Contributors

[![Max13](https://avatars.githubusercontent.com/u/531249?v=4)](https://github.com/Max13 "Max13 (23 commits)")

---

Tags

carbonintervalconditional-statementsphpdateintervalperiodconditional

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/max13-conditional-period/health.svg)

```
[![Health](https://phpackages.com/badges/max13-conditional-period/health.svg)](https://phpackages.com/packages/max13-conditional-period)
```

###  Alternatives

[league/period

Time range API for PHP

7335.4M21](/packages/league-period)[spatie/period

Complex period comparisons

1.7k4.0M10](/packages/spatie-period)[brick/date-time

Date and time library

3623.3M60](/packages/brick-date-time)[digital-creative/conditional-container

Provides an easy way to conditionally show and hide fields in your Nova resources.

116593.8k4](/packages/digital-creative-conditional-container)[izi-dev/nova-conditional-field

Conditional Field

1623.9k](/packages/izi-dev-nova-conditional-field)

PHPackages © 2026

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