Collection of PHP classes to work with timespans.
It differs from DatePeriod in that it only defines a timespan/period by a start and end date. It offers algebraic methods to manipulate a timespan or a collection of them. Convert it to a DatePeriod passing a DateInterval to toPeriod()
when you need to iterate over it.
$ composer require smhg/date-timespan
use Timespan\Timespan;
$start = new DateTime('last monday');
$end = clone $start;
$end->modify('+1 week');
$timespan = new Timespan($start, $end);
Returns whether the timespan contains a date.
Returns whether the timespan overlaps with another timespan.
Returns a collection of timespans (pieces of the original timespan) which do not appear in another timespan.
Returns a collection with the merged timespan or both timespans when a merge was not possible.
Returns a new timespan fitting within the passed boundaries or null
if no time remains.
Returns whether the timespan occurs before, together or after another timespan.
Converts the timespan to a DatePeriod using an interval.
Converts the timespan to a string in ISO 8601 time interval format.
Converts the timespan to an array with a single interval
element containing the toString
representation.
use Timespan\Timespan;
use Timespan\Collection;
$collection = new Collection();
$collection[] = new Timespan(...);
Sorts the collection based on the start of each timespan.
Joins timespans in the collection if they overlap. Also sorts the result.
Merges the collection with another collection. Also compresses the result.