All contributions

spatie/period

spatie/period

A PHP library by Spatie for comparing time periods and collections of them, with precision from years down to seconds and control over whether boundaries are included.

Language
PHP
GitHub stars
1,701
Merged changes
1

What we changed

Period::length() returns 0 for an empty period whose excluded boundaries reverse its start and end, matching the iterator, which yields no dates in that case.

What this work shows

  • Date arithmetic in PHP (DateTimeImmutable)
  • Interval and boundary edge cases
  • Dataset-driven tests in Pest
What was broken

An empty period had a length of 3

A one-day period with Boundaries::EXCLUDE_ALL() is empty: its included start falls after its included end and the iterator yields no dates. length() still measured the distance with diff()->days or abs(), which drop the sign, and returned 3, and the same happened for hours, minutes and seconds.

How we fixed it

length() agrees with the iterator

When the start falls after the end and the iterator yields no dates, length() returns 0 for day, hour, minute and second precision. Tests also make sure non-empty periods with fractional seconds and custom iterators for months and years give the same results as before, and the README documents the rule.

#153

Merged change 1

  1. #153 fix: `Period::length()` returns a positive number for empty (reverse-boundary) periods Merged 27 October 2026

What it taught us

When a class computes the same thing in two ways, we can pick the source of truth, here the iterator, and make the other way agree with it. Tests make sure the fix leaves the results for periods that already worked unchanged.

More about this technology: PHP

Other projects we write code for

Describe your project.

A free quote comes back within an hour during working hours.