Skip to content

Instantly share code, notes, and snippets.

@verschuur
Created November 28, 2019 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verschuur/e15b91fa130e404221242adf9fb532b1 to your computer and use it in GitHub Desktop.
Save verschuur/e15b91fa130e404221242adf9fb532b1 to your computer and use it in GitHub Desktop.
<?php
# Shamelessly stolen from https://stackoverflow.com/questions/1053060/file-put-contents-with-array
# Remember to start $done with 1 or more, or you'll get a division by zero. 🙀
function show_status($done, $total, $size = 30)
{
static $start_time;
// if we go over our bound, just ignore it
if ($done > $total) {
return;
}
if (empty($start_time)) {
$start_time=time();
}
$now = time();
$perc=(double)($done/$total);
$bar=floor($perc*$size);
$status_bar="\r[";
$status_bar.=str_repeat("=", $bar);
if ($bar<$size) {
$status_bar.=">";
$status_bar.=str_repeat(" ", $size-$bar);
} else {
$status_bar.="=";
}
$disp=number_format($perc*100, 0);
$status_bar.="] $disp% $done/$total";
$rate = ($now-$start_time)/$done;
$left = $total - $done;
$eta = round($rate * $left, 2);
$elapsed = $now - $start_time;
$status_bar.= " remaining: ".number_format($eta)." sec. elapsed: ".number_format($elapsed)." sec.";
echo "$status_bar ";
flush();
// when done, send a newline
if ($done == $total) {
echo "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment