League Period: A Swiss Army Knife for Time Intervals in PHP
Originally published at recca0120.github.io Have you ever written this kind of code — checking whether two time intervals overlap? if ($startA < $endB && $startB < $endA) { // overlap...

Source: DEV Community
Originally published at recca0120.github.io Have you ever written this kind of code — checking whether two time intervals overlap? if ($startA < $endB && $startB < $endA) { // overlap } Looks simple, but add boundary conditions (include endpoints or not?), more intervals (what about three?), and gap detection (which time slots aren't covered?), and the code explodes. League Period wraps time intervals into immutable value objects with built-in overlap, containment, gap, and intersection operations. No hand-written logic. Install composer require league/period Requires PHP 8.1+. Creating Intervals From Dates use League\Period\Period; // Basic: specify start and end $meeting = Period::fromDate('2026-04-06 09:00', '2026-04-06 10:30'); // Default is [start, end) — includes start, excludes end From Calendar Units // All of April 2026 $april = Period::fromMonth(2026, 4); // Q1 2026 $q1 = Period::fromQuarter(2026, 1); // Full year 2026 $year = Period::fromYear(2026); // ISO week