Reputation: 29
I'm building a queue to generate something and I want it visible for users to see how long it will take till the generation is ready.
So I have an estimated time it takes to generate something, but that is a variable because it can be anywhere between 5-120 seconds. And now I need to add the variable time to the time of the day and make a loop because the queue has more values.
So for example I 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 so on..
I already 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 but both gives me like this 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 I make it loop that it works?
Edit: This is my loop
$this_time = date('h:i:s',time());
$my_time = $this_time;
$num = 1;
foreach($orders as $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>';
}
Upvotes: 1
Views: 1131
Reputation: 24406
Showing your loop code might help, but here is the general idea of what you should do:
$current_time = time(); // seconds since unix epoch
echo 'Start: ' . date('h:i:s') . PHP_EOL;
while($you_do_stuff == true) {
// do stuff
$now = time();
$time_taken = $now - $current_time;
echo $time_taken . ' seconds to process: ' . date('h:i:s', $now) . PHP_EOL;
// set current time to now
$current_time = $now;
}
echo 'Finished: ' . date('h:i:s');
Edit: here's an example with a 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 as $estimated_time) {
// add estimated time to current time (this increases each loop)
$current_time += $estimated_time;
// output estimated time and finish time
echo 'Estimated time: ' . $estimated_time . ' seconds: ' . date('h:i:s', $current_time) . PHP_EOL;
}
Upvotes: 2