---
title: "spatie/period: length of empty periods in PHP | VulCode"
description: "A fix in spatie/period, the PHP time period library: Period::length() returns 0 for an empty period with excluded boundaries, the same as its iterator."
url: "https://vulcode.com/en/opensource/contributions/spatie-period"
lang: "en"
---

[All contributions](https://vulcode.com/en/opensource/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.

[See the repository](https://github.com/spatie/period) [All contributions](https://vulcode.com/en/opensource/contributions)

- **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](https://github.com/spatie/period/pull/153)

## Merged change 1

- [#153 fix: `Period::length()` returns a positive number for empty (reverse-boundary) periods Merged 27 October 2026](https://github.com/spatie/period/pull/153)

## 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](https://vulcode.com/en/technologies/php)

## Other projects we write code for

- [**Composer** PHP · 29,590 stars The dependency manager for PHP: it installs and updates a project's libraries from its composer.json.](https://vulcode.com/en/opensource/contributions/composer)

- [**PHPMailer** PHP · 22,347 stars The classic PHP library for sending email, used by WordPress and many other systems.](https://vulcode.com/en/opensource/contributions/phpmailer)

- [**Monolog** PHP · 21,407 stars A PHP logging library that sends records to files, sockets, inboxes, databases and web services. Symfony and Laravel use it, among others.](https://vulcode.com/en/opensource/contributions/monolog)

- [**Carbon** PHP · 16,583 stars A PHP DateTime extension for working with dates, standard in the Laravel ecosystem.](https://vulcode.com/en/opensource/contributions/carbon)

- [**Whoops** PHP · 13,231 stars A PHP library that turns uncaught exceptions into a readable error page with the stack trace, known from Laravel.](https://vulcode.com/en/opensource/contributions/whoops)

- [**ramsey/uuid** PHP · 12,642 stars A PHP library for generating universally unique identifiers (UUIDs).](https://vulcode.com/en/opensource/contributions/ramsey-uuid)

## Describe your project.

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

### Rather skip the form?

Ask in the chat or write to us directly.

Open the chat

- **Email**: [hello@vulcode.com](mailto:hello@vulcode.com)
- **Phone**: [+48 576 043 920](tel:+48576043920)
- **Discord**: [discord.gg/vulcode](https://discord.gg/vulcode)
