Created
April 4, 2023 02:44
-
-
Save robjbrain/ae1f011473fc29b0151d0c3e2421572e to your computer and use it in GitHub Desktop.
Improved remaining time calculation for laravel and symfony progress bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//In order to get more accurate remaining time when using Laravel Progress Bar add this to AppServerProvider@boot | |
use Symfony\Component\Console\Helper\ProgressBar; | |
ProgressBar::setPlaceholderFormatterDefinition('remaining', function($bar) { | |
$remaining = $bar->getRemaining(); | |
$hours = floor($remaining / 3600); | |
$minutes = floor(($remaining / 60) % 60); | |
$seconds = $remaining % 60; | |
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds); | |
}); | |
// Here is an example of a progress bar which will take 1 hour and 48 minutes to complete: | |
// Before | |
// 5/6500 [>---------------------------] 0% 5 secs taken 1 hr remaining | |
// After | |
// 5/6500 [>---------------------------] 0% 5 secs taken 01:48:15 remaining | |
// As you can see it's much clearer, now you know you have nearly 2 hours to wait rather than wrongly thinking it will take 1 hour. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment