[英]add variable seconds to time foreach loop
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. 因此,我估算出生成某种东西所需的时间,但这是一个变量,因为它可以介于5-120秒之间。 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 对象1-预计生成时间:15秒-09:00:15
Object 2 - Estimated generation time: 20 sec - 09:00:35 对象2-预计生成时间:20秒-09:00:35
Object 3 - Estimated generation time: 10 sec - 09:00:45 物件3-预计产生时间:10秒-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 对象1-预计生成时间:15秒-09:00:15
Object 2 - Estimated generation time: 20 sec - 09:00:20 对象2-预计生成时间:20秒-09:00:20
Object 3 - Estimated generation time: 10 sec - 09:00:10 对象3-预计生成时间:10秒-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>';
}
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.