Convert Figma logo to code with AI

spatie logoperiod

Complex period comparisons

1,589
72
1,589
1

Top Related Projects

16,515

A simple PHP API extension for DateTime.

1,351

A standalone DateTime library originally based off of Carbon

7,532

Assertions to validate method input/output with nice error messages.

Quick Overview

The spatie/period library is a PHP package that provides a simple and intuitive way to work with time periods. It allows you to create, manipulate, and compare time periods, making it a useful tool for various date and time-related tasks.

Pros

  • Intuitive API: The library offers a clean and easy-to-use API, making it straightforward to work with time periods.
  • Flexible Date/Time Handling: The library supports a wide range of date and time formats, allowing you to work with various input sources.
  • Comprehensive Functionality: The library provides a wide range of features, including period creation, manipulation, comparison, and more.
  • Dependency-free: The spatie/period library is a standalone package and does not require any additional dependencies, making it easy to integrate into your project.

Cons

  • Limited Localization: The library currently only supports English, and adding support for other languages may require additional effort.
  • Potential Performance Impact: Depending on the complexity of your time period operations, the library may have a noticeable performance impact on your application.
  • Lack of Advanced Features: While the library provides a solid set of basic features, it may not offer the most advanced or specialized time period-related functionality.
  • Potential Learning Curve: For developers unfamiliar with the library, the initial setup and understanding of the API may require some time and effort.

Code Examples

use Spatie\Period\Period;

// Creating a period
$period = Period::make('2023-01-01', '2023-12-31');

// Checking if a period is empty
if ($period->isEmpty()) {
    // ...
}

// Checking if a period contains a specific date
if ($period->contains(new \DateTimeImmutable('2023-06-15'))) {
    // ...
}

// Splitting a period into smaller periods
$periods = $period->split(Period::INTERVAL_MONTH);

Getting Started

To get started with the spatie/period library, follow these steps:

  1. Install the package using Composer:
composer require spatie/period
  1. Import the necessary classes in your PHP file:
use Spatie\Period\Period;
use Spatie\Period\Exceptions\InvalidPeriod;
  1. Create a new period using the make() method:
$period = Period::make('2023-01-01', '2023-12-31');
  1. Perform various operations on the period, such as checking its duration, splitting it into smaller periods, or checking if it contains a specific date:
$duration = $period->getDuration(); // Returns a DateInterval object
$subPeriods = $period->split(Period::INTERVAL_MONTH);
$containsDate = $period->contains(new \DateTimeImmutable('2023-06-15'));
  1. Explore the library's documentation for more advanced usage, such as period manipulation, comparison, and custom interval definitions.

Competitor Comparisons

16,515

A simple PHP API extension for DateTime.

Pros of Carbon

  • Extensive set of features and utilities for working with dates and times
  • Widely adopted and well-documented, with a large community and ecosystem
  • Supports a wide range of date and time formats, including custom formats

Cons of Carbon

  • Larger codebase and dependency footprint compared to Period
  • May be overkill for simple date and time operations

Code Comparison

Carbon:

$now = Carbon::now();
$tomorrow = $now->addDay();
$lastWeek = $now->subWeek();

Period:

$now = Period::now();
$tomorrow = $now->add(Period::day());
$lastWeek = $now->sub(Period::week());
1,351

A standalone DateTime library originally based off of Carbon

Pros of Chronos

  • Chronos provides a more comprehensive set of date and time manipulation functions, including support for time zones, daylight saving time, and other advanced features.
  • Chronos has a more intuitive and user-friendly API, making it easier to work with date and time data in your application.
  • Chronos is actively maintained and has a larger community, which means more support and a wider range of use cases.

Cons of Chronos

  • Chronos is primarily designed for use within the CakePHP framework, and may not be as easily integrated into other PHP projects.
  • Chronos has a larger footprint than Period, which may be a concern for projects with strict performance requirements.
  • Chronos may have a steeper learning curve for developers who are not familiar with the CakePHP ecosystem.

Code Comparison

Period:

$period = new Period('2023-01-01', '2023-01-31');
echo $period->getStartDate()->format('Y-m-d'); // Output: 2023-01-01
echo $period->getEndDate()->format('Y-m-d'); // Output: 2023-01-31

Chronos:

$date = new Chronos('2023-01-01');
echo $date->format('Y-m-d'); // Output: 2023-01-01
$date = $date->addDays(30);
echo $date->format('Y-m-d'); // Output: 2023-01-31
7,532

Assertions to validate method input/output with nice error messages.

Pros of webmozarts/assert

  • Provides a wide range of assertion methods, allowing for more expressive and readable tests.
  • Supports type-safe assertions, ensuring that the expected and actual values are of the correct type.
  • Includes helpful error messages that provide detailed information about failed assertions.

Cons of webmozarts/assert

  • May have a steeper learning curve compared to simpler assertion libraries.
  • Requires additional dependencies, which can increase the overall project size.
  • May not be as lightweight or performant as some other assertion libraries.

Code Comparison

spatie/period

$period = Period::create(
    new DateTime('2023-01-01'),
    new DateTime('2023-01-31')
);

$period->contains(new DateTime('2023-01-15')); // true

webmozarts/assert

use Webmozart\Assert\Assert;

Assert::greaterThanEq(
    $value,
    0,
    'The value must be greater than or equal to 0.'
);

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Complex period comparisons

Latest Version on Packagist Quality Score Total Downloads

This package adds support for comparing multiple dates with each other. You can calculate the overlaps and differences between n-amount of periods, as well as some more basic comparisons between two periods.

Periods can be constructed from any type of DateTime implementation, making this package compatible with custom DateTime implementations like Carbon (see cmixin/enhanced-period to convert directly from and to CarbonPeriod).

Periods are always immutable, there's never the worry about your input dates being changed.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/period

Usage

Creating periods

You're encouraged to create periods using their static constructor:

$period = Period::make('2021-01-01', '2021-01-31');

You can manually construct a period, but you'll need to manually provide its precision and boundaries. Using Period::make, the default precision (Precision::DAY()) and default boundaries (Boundaries::EXCLUDE_NONE()) are used.

Before discussing the API provided by this package, it's important to understand both how precision and boundaries are used.

Precision

Date precision is of utmost importance if you want to reliably compare two periods. The following example:

Given two periods: [2021-01-01, 2021-01-15] and [2021-01-15, 2021-01-31]; do they overlap?

At first glance the answer is "yes": they overlap on 2021-01-15. But what if the first period ends at 2021-01-15 10:00:00, while the second starts at 2021-01-15 15:00:00? Now they don't anymore!

This is why this package requires you to specify a precision with each period. Only periods with the same precision can be compared.

A more in-depth explanation on why precision is so important can be found here. A period's precision can be specified when constructing that period:

Period::make('2021-01-01', '2021-02-01', Precision::DAY());

The default precision is set on days. These are the available precision options:

Precision::YEAR()
Precision::MONTH()
Precision::DAY()
Precision::HOUR()
Precision::MINUTE()
Precision::SECOND()

Boundaries

By default, period comparisons are done with included boundaries. This means that these two periods overlap:

$a = Period::make('2021-01-01', '2021-02-01');
$b = Period::make('2021-02-01', '2021-02-28');

$a->overlapsWith($b); // true

The length of a period will also include both boundaries:

$a = Period::make('2021-01-01', '2021-01-31');

$a->length(); // 31

It's possible to override the boundary behaviour:

$a = Period::make('2021-01-01', '2021-02-01', boundaries: Boundaries::EXCLUDE_END());
$b = Period::make('2021-02-01', '2021-02-28', boundaries: Boundaries::EXCLUDE_END());

$a->overlapsWith($b); // false

There are four types of boundary exclusion:

Boundaries::EXCLUDE_NONE();
Boundaries::EXCLUDE_START();
Boundaries::EXCLUDE_END();
Boundaries::EXCLUDE_ALL();

Reference

The Period class offers a rich API to interact and compare with other periods and collections of periods. Take into account that only periods with the same precision can be compared:

  • startsBefore(DateTimeInterface $date): bool: whether a period starts before a given date.
  • startsBeforeOrAt(DateTimeInterface $date): bool: whether a period starts before or at a given date.
  • startsAfter(DateTimeInterface $date): bool: whether a period starts after a given date.
  • startsAfterOrAt(DateTimeInterface $date): bool: whether a period starts after or at a given date.
  • startsAt(DateTimeInterface $date): bool: whether a period starts at a given date.
  • endsBefore(DateTimeInterface $date): bool: whether a period ends before a given date.
  • endsBeforeOrAt(DateTimeInterface $date): bool: whether a period end before or at a given date.
  • endsAfter(DateTimeInterface $date): bool: whether a period ends after a given date.
  • endsAfterOrAt(DateTimeInterface $date): bool: whether a period end after or at a given date.
  • endsAt(DateTimeInterface $date): bool: whether a period starts ends at a given date.
  • overlapsWith(Period $period): bool: whether a period overlaps with another period.
  • touchesWith(Period $other): bool: whether a period touches with another period.
  • contains(DateTimeInterface|Period $other): bool: whether a period contains another period or a single date.
  • equals(Period $period): bool: whether a period equals another period.

On top of comparisons, the Period class also offers a bunch of operations:

overlap(Period ...$others): ?static

Overlaps two or more periods on each other. The resulting period will be the union of all other periods combined.

overlapAny(Period ...$others): PeriodCollection

Overlaps two or more periods on each other. Whenever two or more periods overlap, that overlapping period is added to a collection which will be returned as the final result.

subtract(Period ...$others): PeriodCollection

Subtracts one or more periods from the main period. This is the inverse operation of overlap.

gap(Period $period): ?static

Gets the gap between two periods, or 0 if the periods overlap.

diffSymmetric(Period $other): PeriodCollection

Performs a symmetric diff between two periods.

renew(): static

Renew the current period, creating a new period with the same length that happens after the current period.


Next, the Period class also has some getters:

  • isStartIncluded(): bool
  • isStartExcluded(): bool
  • isEndIncluded(): bool
  • isEndExcluded(): bool
  • start(): DateTimeImmutable
  • includedStart(): DateTimeImmutable
  • end(): DateTimeImmutable
  • includedEnd(): DateTimeImmutable
  • ceilingEnd(Precision::SECOND): DateTimeImmutable
  • length(): int
  • duration(): PeriodDuration
  • precision(): Precision
  • boundaries(): Boundaries

The PeriodCollection class represents a collection of periods and has some useful methods on its own:

overlapAll(PeriodCollection ...$others): PeriodCollection

Overlaps all collection periods on each other.

subtract(PeriodCollection|Period ...$others): PeriodCollection

Subtracts a period or a collection of periods from a period collection.

boundaries(): ?Period

Creates a new period representing the outer boundaries of the collection.

gaps(): static

Gives the gaps for all periods within this collection.

intersect(Period $intersection): static

Intersects given period with every period within a collection. The result is a new collection of overlapping periods between given period and every period in the collection. When there's no overlap, the original period is discarded.

union(): static

Merges all periods in collection with overlapping ranges.


Finally, there are a few utility methods available on PeriodCollection as well:

  • add(Period ...$periods): static
  • map(Closure $closure): static:
  • reduce(Closure $closure, $initial = null): mixed:
  • filter(Closure $closure): static:
  • isEmpty(): bool:

Compatibility

You can construct a Period from any type of DateTime object such as Carbon:

Period::make(Carbon::make('2021-01-01'), Carbon::make('2021-01-02'));

Note that as soon as a period is constructed, all further operations on it are immutable. There's never the danger of changing the input dates.

You can iterate a Period like a regular DatePeriod with the precision specified on creation:

$datePeriod = Period::make(Carbon::make('2021-01-01'), Carbon::make('2021-01-31'));

foreach ($datePeriod as $date) {
    /** @var DateTimeImmutable $date */
    // 2021-01-01
    // 2021-01-02
    // ...
    // (31 iterations)
}

$timePeriod = Period::make(Carbon::make('2021-01-01 00:00:00'), Carbon::make('2021-01-01 23:59:59'), Precision::HOUR());

foreach ($timePeriod as $time) {
    /** @var DateTimeImmutable $time */
    // 2021-01-01 00:00:00
    // 2021-01-01 01:00:00
    // ...
    // (24 iterations)
}

Visualizing periods

You can visualize one or more Period objects as well as PeriodCollection objects to see how they related to one another:

$visualizer = new Visualizer(["width" => 27]);

$visualizer->visualize([
    "A" => Period::make('2021-01-01', '2021-01-31'),
    "B" => Period::make('2021-02-10', '2021-02-20'),
    "C" => Period::make('2021-03-01', '2021-03-31'),
    "D" => Period::make('2021-01-20', '2021-03-10'),
    "OVERLAP" => new PeriodCollection(
        Period::make('2021-01-20', '2021-01-31'),
        Period::make('2021-02-10', '2021-02-20'),
        Period::make('2021-03-01', '2021-03-10')
    ),
]);

And visualize will return the following string:

A          [========]
B                      [==]
C                           [========]
D               [==============]
OVERLAP         [===]  [==] [==]

The visualizer has a configurable width provided upon creation which will control the bounds of the displayed periods:

$visualizer = new Visualizer(["width" => 10]);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2021 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

License

The MIT License (MIT). Please see License File for more information.