php - add variable seconds to time foreach loop -
i'm building queue generate , want visible users see how long take till generation ready.
so have estimated time takes generate something, variable because can anywhere between 5-120 seconds. , need add variable time time of day , make loop because queue has more values.
so example need this:
object 1 - estimated generation time: 15 sec - 09:00:15
object 2 - estimated generation time: 20 sec - 09:00:35
object 3 - estimated generation time: 10 sec - 09:00:45
and on..
i tried this:
$my_time = date('h:i:s',time()); $seconds2add = $estimated_time; $new_time= strtotime($my_time); $new_time+=$seconds2add; echo date('h:i:s',$new_time);
and:
$time = date("m/d/y h:i:s a", time() + $estimated_time);
it loops both gives me output:
object 1 - estimated generation time: 15 sec - 09:00:15
object 2 - estimated generation time: 20 sec - 09:00:20
object 3 - estimated generation time: 10 sec - 09:00:10
so how can make loop works?
edit: loop
$this_time = date('h:i:s',time()); $my_time = $this_time; $num = 1; foreach($orders $order) { echo '<tr>' . '<td>'.($num++).'</td>' . '<td>'. $order->url .'</td>' . '<td>'. $order->product_desc .'</td>' . '<td>'. $order->size .' cm</td>' . '<td>'. $order->estimated_time .' sec</td>'; $seconds2add = $order->estimated_time; $my_time= strtotime($my_time); $my_time+=$seconds2add; echo '<td>'. date('h:i:s',$my_time) . '</td>' . '</tr>'; }
showing loop code might help, here general idea of should do:
$current_time = time(); // seconds since unix epoch echo 'start: ' . date('h:i:s') . php_eol; while($you_do_stuff == true) { // stuff $now = time(); $time_taken = $now - $current_time; echo $time_taken . ' seconds process: ' . date('h:i:s', $now) . php_eol; // set current time $current_time = $now; } echo 'finished: ' . date('h:i:s');
edit: here's example bunch of random "estimated times" in seconds:
// ... in seconds $estimated_times = array( 5, 20, 35, 110 ); $current_time = time(); // seconds since unix epoch echo 'start: ' . date('h:i:s') . php_eol; foreach($estimated_times $estimated_time) { // add estimated time current time (this increases each loop) $current_time += $estimated_time; // output estimated time , finish time echo 'estimated time: ' . $estimated_time . ' seconds: ' . date('h:i:s', $current_time) . php_eol; }
Comments
Post a Comment