Skip to content

Commit 242d5d4

Browse files
committed
Greatly simplified the secondsToHumanTime function
1 parent 6ba4770 commit 242d5d4

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

src/utilities.php

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -160,46 +160,32 @@ function extimatedTimeLeft(float $startmicrotime, float $taskCurrentIteration, f
160160
/**
161161
* Transforms the number of $seconds into minutes, hours and days if needed providing a better human-readable string
162162
* @param float $seconds
163-
* @param bool $zeropad
164163
* @return string
165164
*/
166-
function secondsToHumanTime(float $seconds, bool $zeropad = false): string
165+
function secondsToHumanTime(float $seconds): string
167166
{
168-
$padder = fn(float $item, int $length) => str_pad((string) $item, $length, $zeropad ? '0' : ' ', STR_PAD_LEFT);
167+
static $padder = null;
168+
$padder ??= fn(float $item, int $length) => str_pad((string) $item, $length, ' ', STR_PAD_LEFT);
169169

170-
if ($seconds < 1) {
171-
return $padder(floor($seconds * 1000), 3) . 'ms';
172-
}
173-
174-
if ($seconds < 60) {
175-
return $padder(floor($seconds), 2) . 's';
176-
}
170+
$daysLeft = floor($seconds / 86400);
171+
$hoursLeft = floor(($seconds - ($daysLeft * 86400)) / 3600);
172+
$minutesLeft = floor(($seconds - ($daysLeft * 86400) - ($hoursLeft * 3600)) / 60);
173+
$secondsLeft = floor($seconds - ($daysLeft * 86400) - ($hoursLeft * 3600) - ($minutesLeft * 60));
177174

178-
if ($seconds < 3600) {
179-
$minutesLeft = floor($seconds / 60);
180-
$secondsLeft = floor($seconds - ($minutesLeft * 60));
175+
$output = '';
181176

182-
return $padder($minutesLeft, 2) . 'min'
183-
. ', ' . $padder($secondsLeft, 2) . 's';
177+
if ($daysLeft) {
178+
$output .= $padder($daysLeft, 2) . 'd, ';
184179
}
185180

186-
if ($seconds < 86400) {
187-
$hoursLeft = floor($seconds / 3600);
188-
$minutesLeft = floor(($seconds - ($hoursLeft * 3600)) / 60);
189-
$secondsLeft = floor($seconds - ($hoursLeft * 3600) - ($minutesLeft * 60));
190-
191-
return $padder($hoursLeft, 2) . 'h'
192-
. ', ' . $padder($minutesLeft, 2) . 'min'
193-
. ', ' . $padder($secondsLeft, 2) . 's';
181+
if ($hoursLeft) {
182+
$output .= $padder($hoursLeft, 2) . 'h, ';
194183
}
195184

196-
$daysLeft = floor($seconds / 86400);
197-
$hoursLeft = floor(($seconds - ($daysLeft * 86400)) / 3600);
198-
$minutesLeft = floor(($seconds - ($daysLeft * 86400) - ($hoursLeft * 3600)) / 60);
199-
$secondsLeft = floor($seconds - ($daysLeft * 86400) - ($hoursLeft * 3600) - ($minutesLeft * 60));
185+
if ($minutesLeft) {
186+
$output .= $padder($minutesLeft, 2) . 'm, ';
187+
}
200188

201-
return $padder($daysLeft, 3) . 'd'
202-
. ', ' . $padder($hoursLeft, 2) . 'h'
203-
. ', ' . $padder($minutesLeft, 2) . 'min'
204-
. ', ' . $padder($secondsLeft, 2) . 's';
189+
$output .= $padder($secondsLeft, 2) . 's';
190+
return $output;
205191
}

0 commit comments

Comments
 (0)