function Helper::formatTime
2 calls to Helper::formatTime()
- ProgressBar::initPlaceholderFormatters in vendor/
symfony/ console/ Helper/ ProgressBar.php - ProgressIndicator::initPlaceholderFormatters in vendor/
symfony/ console/ Helper/ ProgressIndicator.php
File
-
vendor/
symfony/ console/ Helper/ Helper.php, line 88
Class
- Helper
- Helper is the base class for all helper classes.
Namespace
Symfony\Component\Console\HelperCode
public static function formatTime(int|float $secs, int $precision = 1) : string {
$secs = (int) floor($secs);
if (0 === $secs) {
return '< 1 sec';
}
static $timeFormats = [
[
1,
'1 sec',
'secs',
],
[
60,
'1 min',
'mins',
],
[
3600,
'1 hr',
'hrs',
],
[
86400,
'1 day',
'days',
],
];
$times = [];
foreach ($timeFormats as $index => $format) {
$seconds = isset($timeFormats[$index + 1]) ? $secs % $timeFormats[$index + 1][0] : $secs;
if (isset($times[$index - $precision])) {
unset($times[$index - $precision]);
}
if (0 === $seconds) {
continue;
}
$unitCount = $seconds / $format[0];
$times[$index] = 1 === $unitCount ? $format[1] : $unitCount . ' ' . $format[2];
if ($secs === $seconds) {
break;
}
$secs -= $seconds;
}
return implode(', ', array_reverse($times));
}