This PHP package provides classes for immutable objects to represent date ranges and methods to manipulate them.
You can install the package via composer:
composer require swisnl/date-rangeSwis\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.
composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
If you discover any security related issues, please email security@swis.nl instead of using the issue tracker.
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 is a web agency from Leiden, the Netherlands. We love working with open source software.