如何在数组中累计时间? [英] How to sum time in array?
本文介绍了如何在数组中累计时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
// print_r($ chart_average);
Array([0] => 00:20:00 [1] => 00:03:45 [2])
如何总结数组中的所有时间并显示如下所示的结果。如何计算所有数组?
总时间:00:23:45
我的答案供我参考后这:
foreach($ chart_average as $ time){
list($ hour,$ minute,$ second) = explode(':',$ time);
$ all_seconds + = $ hour * 3600;
$ all_seconds + = $ minute * 60;
$ all_seconds + = $ second;
}
$ total_minutes = floor($ all_seconds / 60);
$秒= $ all_seconds%60;
$ hours = floor($ total_minutes / 60);
$分钟= $ total_minutes%60;
//返回已经格式化的时间
echo sprintf('%02d:%02d:%02d',$ hours,$ minutes,$ seconds);
解决方案
$ times = array();
$ times [] =12:59;
$ times [] =0:58;
$ times [] =0:02;
//将数组传递给函数
echo AddPlayTime($ times);
函数AddPlayTime($ times){
//循环遍历所有时间
foreach($ times as $ time){
list($小时,分钟,$秒)= explode(':',$ time);
$ all_seconds + = $ hour * 3600;
$ all_seconds + = $ minute * 60; $ all_seconds + = $ second;
}
$ total_minutes = floor($ all_seconds / 60); $秒= $ all_seconds%60; $ hours = floor($ total_minutes / 60); $分钟= $ total_minutes%60;
//返回已格式化的时间
返回sprintf('%02d:%02d:%02d',$小时,$分钟,$秒);
}
i have an array. i want to sum all time in the array so it will result sum of all time in the array. :
//print_r($chart_average);
Array ( [0] => 00:20:00 [1] => 00:03:45 [2])
how to sum all the time in array above and show result like below. How to calculate all array?
total time : 00:23:45
my answer for my reference after this :
foreach ($chart_average as $time) {
list($hour, $minute, $second) = explode(':', $time);
$all_seconds += $hour * 3600;
$all_seconds += $minute * 60;
$all_seconds += $second;
}
$total_minutes = floor($all_seconds/60);
$seconds = $all_seconds % 60;
$hours = floor($total_minutes / 60);
$minutes = $total_minutes % 60;
// returns the time already formatted
echo sprintf('%02d:%02d:%02d', $hours, $minutes,$seconds);
解决方案
$times = array();
$times[] = "12:59";
$times[] = "0:58";
$times[] = "0:02";
// pass the array to the function
echo AddPlayTime($times);
function AddPlayTime($times) {
// loop throught all the times
foreach ($times as $time) {
list($hour, $minute, $second) = explode(':', $time);
$all_seconds += $hour * 3600;
$all_seconds += $minute * 60; $all_seconds += $second;
}
$total_minutes = floor($all_seconds/60); $seconds = $all_seconds % 60; $hours = floor($total_minutes / 60); $minutes = $total_minutes % 60;
// returns the time already formatted
return sprintf('%02d:%02d:%02d', $hours, $minutes,$seconds);
}
这篇关于如何在数组中累计时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文