gpt4 book ai didi

php - php中以小时、分钟和秒为单位的平均时间

转载 作者:可可西里 更新时间:2023-11-01 00:47:29 24 4
gpt4 key购买 nike

我有一些总小时数,需要计算平均值。例如,我的总小时值为 2452:43:44 (H:m:s),总计数为 15。我想以相同的格式获取平均时间,即小时:分钟:秒。我们如何在 PHP 中做到这一点?

最佳答案

function average_time($total, $count, $rounding = 0) {
$total = explode(":", strval($total));
if (count($total) !== 3) return false;
$sum = $total[0]*60*60 + $total[1]*60 + $total[2];
$average = $sum/(float)$count;
$hours = floor($average/3600);
$minutes = floor(fmod($average,3600)/60);
$seconds = number_format(fmod(fmod($average,3600),60),(int)$rounding);
return $hours.":".$minutes.":".$seconds;
}
echo average_time("2452:43:44", 15); // prints "163:30:55"
echo average_time("2452:43:44", 15, 2); // prints "163:30:54.93"

关于php - php中以小时、分钟和秒为单位的平均时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14707964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com