我需要创建一个代码,它将取$ time [1]的值并减去$ time [2]的值,然后取$ time [3]的值并减去$ time的值[4]等等,直到它耗尽了$ time记录。它应该保持运行总量。
模拟数据:
$time[1] = "2017-08-28 18:30:00";
$time[2] = "2017-08-28 14:00:00";
$time[3] = "2017-08-28 13:00:00";
$time[4] = "2017-08-28 12:45:00";
$time[5] = "2017-08-28 12:30:00";
$time[6] = "2017-08-28 12:00:00";
例如:[1] - [2] = 4.5小时,[3] - [4] = 0.25小时,[5] - [6] = 0.5小时;总计= 5.25小时。
$hourdiff = round((strtotime($time[1]) - strtotime($time[2]))/3600, 1); //gives you the quantity of hours worked between 2 records
我需要一种方法来选择记录1和2,计算$ hourdiff,将其添加到运行总计,然后选择记录3和4,计算$ hourdiff,将其添加到运行总计等,直到有没有记录可以计算。
答案 0 :(得分:1)
听起来你正在寻找一个for
循环:
for ($i = 1; $i <= count($time) - 1; $i++) {
$hourdiff = round((strtotime($time[$i + 1]) - strtotime($time[$i]))/3600, 1);
echo "The difference is: $hourdiff" . "<br />";
}
这将回应五个时间上的差异。
请注意,您需要从$i
开始,因为您无法从第一个事件中减去“无”!要解决此问题,您需要将索引停在计数之外。
可以看到 here 。
希望这有帮助! :)
答案 1 :(得分:0)
遍历数组,但不是向迭代变量添加1,而是添加两个(并检查越界错误):
<?php
$time[0] = "2017-08-28 18:30:00";
$time[1] = "2017-08-28 14:00:00";
$time[2] = "2017-08-28 13:00:00";
$time[3] = "2017-08-28 12:45:00";
$time[4] = "2017-08-28 12:30:00";
$time[5] = "2017-08-28 12:00:00";
$total = 0;
for ($i = 0; $i < count($time); $i += 2) {
if ($i + 1 > count($time) - 1) break; // check of out of bounds
$total += round((strtotime($time[$i]) - strtotime($time[$i + 1]))/3600, 1); // add diff of next two numbers
}
echo $total;
答案 2 :(得分:0)
尝试:
$time[1] = "2017-08-28 18:30:00";
$time[2] = "2017-08-28 14:00:00";
$time[3] = "2017-08-28 13:00:00";
$time[4] = "2017-08-28 12:45:00";
$time[5] = "2017-08-28 12:30:00";
$time[6] = "2017-08-28 12:00:00";
$times = array();
$total = 0;
foreach ($time as $n => $t0) {
if ($n % 2) {
continue;
}
$t1 = strtotime($time[$n - 1]);
$t2 = strtotime($t0);
$k = sprintf('%s-%s', $n - 1, $n);
$diff = ($t1 - $t2) / 3600;
$times[$k] = sprintf('%.2f hours', $diff);
$total += $diff;
}
print_r($times);
printf('TOTAL : %s hours', $total);
输出
Array
(
[1-2] => 4.50 hours
[3-4] => 0.25 hours
[5-6] => 0.50 hours
)
TOTAL : 5.25 hours
或者你可以试试DateTime::diff
$time[1] = "2017-08-28 18:30:00";
$time[2] = "2017-08-28 14:00:00";
$time[3] = "2017-08-28 13:00:00";
$time[4] = "2017-08-28 12:45:00";
$time[5] = "2017-08-28 12:30:00";
$time[6] = "2017-08-28 12:00:00";
$times = array();
$total = new DateTime;
foreach ($time as $n => $t0) {
if ($n % 2) {
continue;
}
$t1 = new DateTime($time[$n - 1]);
$t2 = new DateTime($t0);
$k = sprintf('%s-%s', $n - 1, $n);
$diff = $t1->diff($t2);
$times[$k] = $diff->format('%h hour(s) %i min(s) %s sec(s)');
$total = $total->add($diff);
}
$total = ($total->diff(new DateTime))->format('%h hour(s) %i min(s) %s sec(s)');
print_r($times);
printf('TOTAL : %s', $total);
输出
Array
(
[1-2] => 4 hour(s) 30 min(s) 0 sec(s)
[3-4] => 0 hour(s) 15 min(s) 0 sec(s)
[5-6] => 0 hour(s) 30 min(s) 0 sec(s)
)
TOTAL : 5 hour(s) 15 min(s) 0 sec(s)
答案 3 :(得分:0)
使用array_chunk
分组成对,然后array_reduce
对差异求和:
$total = array_reduce(
array_chunk($time, 2),
function ($total, $pair) {
return $total + (strtotime($pair[0]) - strtotime($pair[1]));
},
0
);
输入$time
是一组类似时间的字符串,适合传递给strtotime
。结果$total
将是整数秒。