Skip to content

Instantly share code, notes, and snippets.

@tott
Created December 14, 2011 12:56
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 tott/1476469 to your computer and use it in GitHub Desktop.
Save tott/1476469 to your computer and use it in GitHub Desktop.
Print a simple progress bar for your CLI PHP scripts
<?php
$time1 = time(); // set the start time
dosomethingnasty();
function dosomethingnasty() {
for( $i=0; $i < 300; $i++ ) {
progress( $i, 300, 40 );
sleep( 1 );
}
}
/**
* Print simple progress bar.
*
* @access public
* @param int $processed amount of items processed
* @param int $max maximum amount of items to process
* @return void
*/
function progress( $processed, $max ) {
global $time1;
$progress = round( $processed / ( $max / 100 ), 2);
$progress_points = floor($progress/2);
$time_x=time();
$timediff = $time_x - $time1;
$estimation = round( ( ( ( 100 / $progress * $timediff ) - $timediff ) / 60 ), 2 );
echo str_pad( str_repeat( "#", $progress_points ), 52, " ", STR_PAD_RIGHT) . sprintf( "%.2f", $progress ) . str_pad( "% ( ". sprintf( "%.2f", $estimation ) . " min left )", 27, " ", STR_PAD_RIGHT). "\r" ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment