1

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>';        
} 
3
  • 2
    Your first line is wrong... You're doing all estimates by adding them to "Now" - not to when the previous item finished - eg now +15, now +20 etc. What you want is Now + 15, Previous finish +20, Previous finish +10 Commented Mar 10, 2014 at 8:05
  • @Basic that is indeed exactly what I want. But I can't figure out how to do it. What is wrong with my frist line? Commented Mar 10, 2014 at 8:32
  • When I outcomment the new_time= strtotime($my_time); it works, but makes the time 01:00 and not the current time.. Commented Mar 10, 2014 at 8:47

1 Answer 1

2

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;
}

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

Where is the estimated time added?
It's a generic example, if you post your loop code I'll show you how to work it in.
I added you code to my loop and edited a bit. Its working like a charm now. Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.