多次添加PHP

时间:2018-05-02 16:14:07

标签: php datetime time

我正在为员工构建在线时间表。在时间表的最后,我需要显示总小时数:分钟工作。

我把时间作为变量:

$MondaysActual 
$TuesdaysActual 
$WednesdaysActual 
$ThursdaysActual 
$FridaysActual 

所持数据类似于07:20 - 员工工作时间为7小时20分钟

如何简单地将这些共同添加5次并将总数显示为小时:分钟?

1 个答案:

答案 0 :(得分:2)

试试这个

   $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);
}