elephant-php/duration

Duration is a small immutable value object representing elapsed time.

Maintainers

Package info

github.com/elephant-php/duration

pkg:composer/elephant-php/duration

Statistics

Installs: 5

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.1.0 2026-04-21 15:51 UTC

This package is not auto-updated.

Last update: 2026-04-27 08:42:28 UTC


README

elephant-php

Duration

A small immutable value object representing elapsed time.

Latest Version License PHP 8.1+


Use cases

  • Measuring execution time
  • Logging
  • Reporting
  • Working with time differences

Installation

composer require elephant-php/duration

Creation

from dates

use ElephantPhp\Duration\Duration;

$start    = new DateTimeImmutable('2026-01-01 10:00:00');
$end      = new DateTimeImmutable('2026-01-01 11:01:01');

$duration = Duration::between($start, $end);

from seconds

$duration = Duration::fromSeconds(3661);

from since to now

$duration = Duration::since($startedAt);

Access values

$duration->hours();
$duration->minutes();
$duration->seconds();

⚠️ Note minutes() and seconds() return remaining parts (0–59), not total values.

Total values

$duration->toTotalSeconds();
$duration->toTotalMinutes();
$duration->toTotalHours();

Formatting

// arguments are labels
$duration->format('h', 'm', 's'); // "1 h, 1 m, 1 s"

$duration->toHms();              // "01:01:01"

Example

$duration = Duration::fromSeconds(3661);

echo $duration->format('h', 'm', 's'); // 1 h, 1 m, 1 s
echo $duration->toHms();               // 01:01:01

echo $duration->seconds();             // 1
echo $duration->minutes();             // 1
echo $duration->hours();               // 1

echo $duration->toTotalSeconds();      // 3661
echo $duration->toTotalMinutes();      // 61
echo $duration->toTotalHours();        // 1

License

MIT License

Please see LICENSE for more information.