<?php
function formatDuration(int $seconds): string {
if ($seconds < 0) return '-' . formatDuration(-$seconds);
if ($seconds === 0) return '0s';
$units = ['d' => 86400, 'h' => 3600, 'm' => 60, 's' => 1];
$parts = [];
foreach ($units as $label => $size) {
if ($seconds >= $size) {
$n = intdiv($seconds, $size);
$seconds %= $size;
$parts[] = $n . $label;
}
}
return implode(' ', $parts);
}
echo formatDuration(75); // 1m 15s
echo formatDuration(3725); // 1h 2m 5s
echo formatDuration(90000); // 1d 1h
Create a free account and build your private vault. Share publicly whenever you want.