如何将两个时间相加超过24小时

时间:2019-12-02 10:41:11

标签: php time

我必须编写一个php函数来添加2个超过24小时的时间变量。 时间功能

let a = {
    "id": 0,
    "name": "Root Entity",
    "children": [{
        "id": 1,
        "name": "REAT",
        "children": [{
            "id": 2,
            "name": "Business",
            "children": [{
                "id": 3,
                "name": "Region 1",
                "children": [{
                    "id": 5,
                    "name": "Area 1",
                    "children": [
                    {
                        "id": 14,
                        "name": "lead 1"
                    }, 
                    {
                        "id": 15,
                        "name": "lead 2"
                    }, 
                    {
                        "id": 16,
                        "name": "lead 3"
                    }, 
                    {
                        "id": 17,
                        "name": "lead 4"
                    }, 
                    {
                        "id": 18,
                        "name": "lead 5"
                    }, 
                    {
                        "id": 19,
                        "name": "lead 6"
                    }, {
                        "id": 20,
                        "name": "lead 7"
                    }],
                    "kpi_1_met": 0,
                    "lead_num": 0,
                }, {
                    "id": 6,
                    "name": "Area 2",
                    "children": [{
                        "id": 31,
                        "name": "lead 1"
                    }] 
                }]
            }]
        }]
    }]

}


function test(data) {
            let response = [];
            if (Array.isArray(data)) {
                for (let o of data) {
                    response.push({ id: o.id, name: o.name });
                    if (o.hasOwnProperty('children') && o.children.length > 0) {
                        let child = test(o.children);
                        response = response.concat(child);
                    }
                }
            } else {
                response.push({ id: data.id, name: data.name });
                if (data.hasOwnProperty('children') && data.children.length > 0) {
                    let child = test(data.children);
                    response = response.concat(child);
                }
            }
            return response;
        }
   
        let res = test(a);
        console.log(res)

但是问题是 function TimeSum($a,$b) { list ($hour1, $min1, $sec1) = explode(':', $a); list ($hour2, $min2, $sec2) = explode(':', $b); $sumHour = sprintf('%02d', $hour1 + $hour2); $sumMin = sprintf('%02d', $min1 + $min2); $sumSec = sprintf('%02d', $sec1 + $sec2); return $sumHour.':'.$sumMin.':'.$sumSec; } $a=50:15:00的总和显示为$b=22:55:00而不是72:70:00。如何解决?

2 个答案:

答案 0 :(得分:1)

您的代码中存在逻辑错误,您只是将时间的每个部分相加即可,但是您需要做的是:

如果秒数大于60,则只需获取$ number_seconds%60的结果,并将其添加到分钟数中,然后重复分钟数,让我向您展示:

       function TimeSum($a,$b)
          {
            list ($hour1, $min1, $sec1) = explode(':', $a);
            list ($hour2, $min2, $sec2) = explode(':', $b);
//counting number of seconds and getting extra minutes outs
            $total_sec = $sec1+$sec2;
            $sumSec = $total_sec%60;
            $extra_min = ($total_sec-$sumSec)/60;
//counting number of minutes and getting extra hours outs
            $total_min= $min1+$min2+$extra_min;
            $sumMin = $total_min%60;
            $extra_hr = ($total_min-$sumMin)/60;
//counting number of hours
            $sumHour = $hour1 + $hour2 + $extra_hr;

            return $sumHour.':'.$sumMin.':'.$sumSec;
          }

答案 1 :(得分:0)

您可以使用strtotime将分钟和秒作为unix时间戳,并仅计算小时数。我确定有一个更好/更漂亮的解决方案,但是这个开箱即用:

function TimeSum( $a, $b ) {

  list ( $hourA, $minA, $secA ) = explode( ':', $a );
  list ( $hourB, $minB, $secB ) = explode( ':', $b );

  $timeStamp = strtotime( '1970-01-01 00:' . $minA . ':' . $secA );
  $timeStamp += strtotime( '1970-01-01 00:' . $minB . ':' . $secB );

  $minutesAndSeconds = date( ':i:s', $timeStamp );
  $hours = date( 'H', $timeStamp );

  return sprintf( '%02d', $hours + $hourA + $hourB ) . $minutesAndSeconds;

}