Created
December 14, 2011 12:56
-
-
Save tott/1476469 to your computer and use it in GitHub Desktop.
Print a simple progress bar for your CLI PHP scripts
This file contains hidden or 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
<?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