计算时差并将结果汇总到PHP中 [英] Calculate time difference and sum the results in PHP
本文介绍了计算时差并将结果汇总到PHP中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含字符串形式的时间数据的数组,如下所示:
I have an array contains time data in string, like this:
$time = [
"10:05:32",
"11:03:43",
"13:43:16",
"10:17:21"
];
现在我要计算 $ time [x]-10:00:00
,并对所有结果求和: 00:05:32 + 01:03:43 + 03:43:16 + 00:17:21
(= 5:09:52)
Now I want to calculate $time[x] - 10:00:00
, and sum all the results: 00:05:32 + 01:03:43 + 03:43:16 + 00:17:21
(=5:09:52)
我已经在php.net中阅读了一些参考资料,但是所有内容都是关于日期的,没有简单的时间计算解决方案吗?
I have read some reference in php.net, but everything is about date, there is no a solution for simple time calculation?
推荐答案
我要做的是先将它们隐式标记为时间戳,然后映射差异,然后将其汇总.像这样:
What I would do is covert them to timestamp first, then map the differences then sum them up. Like this:
$diff = '10:00:00';
$time = ["10:05:32", "11:03:43", "13:43:16", "10:17:21"];
$time = array_map(function($t) use ($diff){
return strtotime($t) - strtotime($diff);
}, $time);
$time = gmdate('H:i:s', array_sum($time));
echo $time; // 05:09:52
这篇关于计算时差并将结果汇总到PHP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文