How long is this going to take?

If you are like me you've got a command line script that has to process through thousands of things and you like know how far along it is and how much longer it is going to take. Today I crated this general function to help me monitor the status of where I am with my process and I though making some type of timer would be cool too. Have fun with it.

So to use this function you just need to pass it

$i: Current number you are processing (in my case an array so 0 really is 1
$c: Total number of items to do
$m: any type of message or item number that is being processed
$noTiming: which turns off the timing functionality if you really don't care to see how long or how much furter you have to go.

list($usec, $sec) = explode(' ', microtime());
$script_start = (float) $sec + (float) $usec;

function outputStatus( $i, $c, $m, $noTiming = false )
{
	global $script_start;
	/**	Current Number	**/
	$i++;
	$i = str_pad( $i, strlen($c), ' ', STR_PAD_LEFT );
	
	/**	Percent Done	**/
	$p = str_pad( number_format( round( $i/$c*100, 2 ), 2 ), 5, ' ', STR_PAD_LEFT );
	
	/**	Show Timing Status	**/
	if( !$noTiming )
	{
		/**	Elapsed Time	**/
		list($usec, $sec) = explode(' ', microtime());
		$script_end = (float) $sec + (float) $usec;
		$elapsed_time = round( $script_end - $script_start, 0);
		$elapsed_min = floor( $elapsed_time/60 );
		$elapsed_sec = str_pad( $elapsed_time - ( $elapsed_min * 60 ), 2, '0', STR_PAD_LEFT );
		
		/**	Time Left	**/
		$left_time = $elapsed_time / $i * $c - $elapsed_time;
		$left_min = floor( $left_time/60 );
		$left_sec = str_pad( round( $left_time - ( $left_min * 60 ),0), 2, '0', STR_PAD_LEFT );
		$left_min = str_pad( $left_min, 2, ' ', STR_PAD_LEFT );
		$timing = "Time: ($elapsed_min:$elapsed_sec) - Left: ($left_min:$left_sec) ";
	}
	print "$i of $c ($p%) $timing'$m'\n";
	
}

Test Output

525 of 785 (66.88%) Time: (5:46) - Left: ( 2:51) '40105'
526 of 785 (67.01%) Time: (5:47) - Left: ( 2:51) '20411'
527 of 785 (67.13%) Time: (5:47) - Left: ( 2:50) '30001'
528 of 785 (67.26%) Time: (5:48) - Left: ( 2:49) '65053'
529 of 785 (67.39%) Time: (5:49) - Left: ( 2:49) '65068'
530 of 785 (67.52%) Time: (5:49) - Left: ( 2:48) '65035'
531 of 785 (67.64%) Time: (5:50) - Left: ( 2:47) '30296'
532 of 785 (67.77%) Time: (5:51) - Left: ( 2:47) '30309'
533 of 785 (67.90%) Time: (5:51) - Left: ( 2:46) '65152'
534 of 785 (68.03%) Time: (5:52) - Left: ( 2:45) '30481'
535 of 785 (68.15%) Time: (5:52) - Left: ( 2:44) '30090'
536 of 785 (68.28%) Time: (5:53) - Left: ( 2:44) '65154'
537 of 785 (68.41%) Time: (5:54) - Left: ( 2:43) '30042'
538 of 785 (68.54%) Time: (5:54) - Left: ( 2:43) '30083'
539 of 785 (68.66%) Time: (5:55) - Left: ( 2:42) '30227'
540 of 785 (68.79%) Time: (5:56) - Left: ( 2:42) '65153'
541 of 785 (68.92%) Time: (5:56) - Left: ( 2:41) '65054'
542 of 785 (69.04%) Time: (5:57) - Left: ( 2:40) '30182'
543 of 785 (69.17%) Time: (5:58) - Left: ( 2:40) '30226'
544 of 785 (69.30%) Time: (5:58) - Left: ( 2:39) '65048'
545 of 785 (69.43%) Time: (5:59) - Left: ( 2:38) '30357'
546 of 785 (69.55%) Time: (6:00) - Left: ( 2:38) '30310'
547 of 785 (69.68%) Time: (6:00) - Left: ( 2:37) '15107'
548 of 785 (69.81%) Time: (6:01) - Left: ( 2:36) '30004'
549 of 785 (69.94%) Time: (6:02) - Left: ( 2:36) '30016'
550 of 785 (70.06%) Time: (6:02) - Left: ( 2:35) '30093'
551 of 785 (70.19%) Time: (6:03) - Left: ( 2:34) '65056'
Powered by Drupal, an open source content management system