swisnl/date-range

A PHP package for manipulating date ranges.

Fund package maintenance!
swisnl

1.0.0 2025-08-15 20:30 UTC

This package is auto-updated.

Last update: 2025-08-15 20:33:01 UTC


README

Latest Version on Packagist Software License Buy us a tree Made by SWIS

This PHP package provides classes for immutable objects to represent date ranges and methods to manipulate them.

Installation

You can install the package via composer:

composer require swisnl/date-range

Usage

Swis\DateRange\DateRange is the main class to represent a date range. It is an immutable object that has a start and end date (both optional to support open ended ranges), and provides methods to manipulate the range. Because manipulating date ranges can easily lead to multiple separate date ranges, the package also provides a Swis\DateRange\DateRangeSet class to represent a collection of date ranges. This class also provides methods to manipulate set of date ranges. Both classes are immutable, so any manipulation will return a new instance of the class.

use Swis\DateRange\DateRange;

$range = DateRange::make('2023-01-01', '2023-01-31');
$range->inRange('2023-01-15'); // true
$range->inRange('2023-02-01'); // false

$range2 = DateRange::make('2023-01-15', '2023-01-20');
$range->overlaps($range2); // true
$range->intersect($range2)->toArray(); // ['2023-01-15', '2023-01-20']
$range->subtract($range2)->toArray(); // [['2023-01-01', '2023-01-14'], ['2023-01-21', '2023-01-31']]

$set = $range->subtract($range2);
$set->addDateRange(DateRange::make('2023-01-10', null)); // [['2023-01-01', null]]

For all the available methods, please refer to the PHPDoc documentation in the source code.

Testing

composer test

Changelog

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

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email security@swis.nl instead of using the issue tracker.

Credits

License

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

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

SWIS ❤️ Open Source

SWIS is a web agency from Leiden, the Netherlands. We love working with open source software.