Skip to content

Commit 8400156

Browse files
committed
[ADD] niceEta - Format ETA seconds into human-readable format.
1 parent d3468a8 commit 8400156

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

core/functions/helper.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,40 @@ function nicesize($size)
160160
}
161161
}
162162

163+
if (!function_exists('niceEta')) {
164+
/**
165+
* Format ETA seconds into human-readable format.
166+
*
167+
* Converts seconds into a user-friendly time format:
168+
* - Less than 60 seconds: "45s"
169+
* - Less than 1 hour: "5m 30s"
170+
* - 1 hour or more: "2h 15m"
171+
*
172+
* @param float $seconds Number of seconds to format
173+
* @return string Human-readable time format
174+
*
175+
* @example
176+
* niceEta(45.5); // "46s"
177+
* niceEta(150); // "2m 30s"
178+
* niceEta(3600); // "1h 0m"
179+
* niceEta(8100); // "2h 15m"
180+
*/
181+
function niceEta(float $seconds): string
182+
{
183+
if ($seconds < 60) {
184+
return sprintf('%.0fs', $seconds);
185+
} elseif ($seconds < 3600) {
186+
$minutes = floor($seconds / 60);
187+
$remainingSeconds = $seconds % 60;
188+
return sprintf('%.0fm %.0fs', $minutes, $remainingSeconds);
189+
} else {
190+
$hours = floor($seconds / 3600);
191+
$minutes = floor(($seconds % 3600) / 60);
192+
return sprintf('%.0fh %.0fm', $hours, $minutes);
193+
}
194+
}
195+
}
196+
163197
if (!function_exists('data_is_json')) {
164198
/**
165199
* @param $string

0 commit comments

Comments
 (0)