我有一个名为$ time的变量数组。而我想要做的是计算在用户停留在某些部门,例如秒数dept的无8.PHP中的多维数组
$time = array(
'91' => array(
'100' => array(
'0' =>array(
'id' =>'15',
'time' => '2014/05/28 00:23:26',
'dept' => '8'
),
'1' =>array(
'id' =>'15',
'time' => '2014/05/28 00:25:51',
'dept' => '8'
),
'2' =>array(
'id' =>'15',
'time' => '2014/05/28 00:27:45',
'dept' => '9'
),
'3' =>array(
'id' =>'15',
'time' => '2014/05/28 00:28:01',
'dept' => '8'
)
'4' =>array(
'id' =>'15',
'time' => '2014/05/28 00:30:46',
'dept' => '4'
)
)
);
我有如下表:enter image description here
23:26至25 :51有一个145秒的差异。
25:51至27:45具有114秒的差异。
28:01至30:46具有165秒的差异。
所以,如果我们添加它们145+ 114 + 166
。用户在该部门停留的总秒数为424
秒。
而在第9部分: 27:45至28:01有16秒的差异。
我想达到的输出:
$results = array(
'8' => '424',
'9' => '16'
);
它快把我逼疯了。有人可以帮助我如何实现它,或者实现它的最佳方式是什么。我试过了:
$timeFirst = strtotime('2016/05/26 00:27:45');
$timeSecond = strtotime('2016/05/26 00:28:01');
$differenceInSeconds = $timeSecond - $timeFirst;
print_r($differenceInSeconds);
但问题是我不能继续,因为我不知道如何继续它。谢谢。
你应该看一下[Carbon](http://carbon.nesbot.com/docs/#api-difference)。碳在我的生命中拯救了许多分钟。在你的情况下,你可以使用$ date1-> diffInSeconds($ date2)来获得上述结果。 –