elephant-php / duration
Duration is a small immutable value object representing elapsed time.
0.1.0
2026-04-21 15:51 UTC
Requires
- php: ^8.1
This package is not auto-updated.
Last update: 2026-04-27 08:42:28 UTC
README
Duration
A small immutable value object representing elapsed time.
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()andseconds()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.