您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何求和N次(HH:MM格式)?

如何求和N次(HH:MM格式)?

这应该可以满足您的需求:

$times 是时间的数组,您可以添加所需的时间

$times = array();

$times[] = "12:59";
$times[] = "0:58";
$times[] = "0:02";

// pass the array to the function
echo AddPlayTime($times);

function AddPlayTime($times) {
    $minutes = 0; //declare minutes either it gives Notice: Undefined variable
    // loop throught all the times
    foreach ($times as $time) {
        list($hour, $minute) = explode(':', $time);
        $minutes += $hour * 60;
        $minutes += $minute;
    }

    $hours = floor($minutes / 60);
    $minutes -= $hours * 60;

    // returns the time already formatted
    return sprintf('%02d:%02d', $hours, $minutes);
}

我用正确的变量名称编辑了代码。现在更正确了。

希望这可以帮助 :-)

其他 2022/1/1 18:17:32 有403人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶