joanfabregat/iso8601-to-seconds

Converts ISO 8601 duration to seconds

Maintainers

Package info

github.com/joanfabregat/iso8601-to-seconds

pkg:composer/joanfabregat/iso8601-to-seconds

Statistics

Installs: 64

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v2.0 2026-04-15 18:58 UTC

This package is auto-updated.

Last update: 2026-04-15 18:59:15 UTC


README

Latest Stable Version Total Downloads License CI

A simple PHP library to convert ISO 8601 durations to seconds.

Accepts both ISO 8601 duration strings (e.g. PT1H30M) and DateInterval objects.

Requirements

  • PHP >= 8.2

Installation

composer require joanfabregat/iso8601-to-seconds

Usage

use JoanFabregat\Iso8601ToSeconds\Iso8601ToSeconds;

// From an ISO 8601 string
$seconds = Iso8601ToSeconds::convert('PT1H');
echo $seconds; // 3600

// From a DateInterval object
$interval = new DateInterval('PT1H');
$seconds = Iso8601ToSeconds::convert($interval);
echo $seconds; // 3600

// Combined durations
$seconds = Iso8601ToSeconds::convert('P1DT2H30M15S');
echo $seconds; // 95415

// Invalid input throws \InvalidArgumentException
Iso8601ToSeconds::convert('not a duration'); // throws InvalidArgumentException

License

This library is published under the MIT license (see LICENSE).