-1

我在添加时间时遇到问题,这是不正确的。示例:00:00 + 00:35 = 11:13

它应该是 00:35 而不是 11:13

这是我上面的代码:

echo date('H:i',$total).' + '.$telat2.' = ';

if($telat2 == '00:00'){
    $total = $total;}
else{
    $total = ($total) + strtotime($telat2);}

echo date('H:i',$total).' ';

我希望这里的每个人都可以帮助我..

提前致谢..

更新 1

我刚刚得到了正确的代码!这是代码:

$total_unix = strtotime(date('Y-m-d').' '.$total.':00');

$telat2_unix = strtotime(date('Y-m-d').' '.$telat2.':00');

$begin_day_unix = strtotime(date('Y-m-d').' 00:00:00');

$total = date('H:i', ($total_unix + ($telat2_unix - $begin_day_unix)));

我想知道这怎么会发生?

有人可以向我解释一下吗?

4

1 回答 1

0

为原始客户制作了一个工作代码:

 <?php

    function AddTime($telat1,$telat2)
    {
        $telat1 = date_parse_from_format("H:i", $telat1);
        $telat2 = $timeToAdd;
        $telat2 = date_parse_from_format("H:i", $telat2);


        $totalHours = $telat1['hour'] + $telat2['hour'];
        $totalMinutes = $telat1['minute'] + $telat2['minute'];
        if($totalMinutes >= 60)
        {
            $totalHours += 1;
            $totalMinutes -= 60;
        }
        if($totalHours >= 24)
        {
            $totalHours -= 24;
        }

        // Setting the leading zeros

        if(strlen($totalMinutes) < 2)
        {
            $totalMinutes = "0" . $totalMinutes;
        }
                if(strlen($totalHours) < 2)
        {
            $totalHours = "0" . $totalHours;
        }

        $total = $totalHours . ":" . $totalMinutes;

        return $total;
   }
   $timeNow = date('H:i'); // Base time
   $timeToAdd = '12:12'; // 12 hours 12 minutes to be added to $timeNow
   $total = AddTime($timeNow, $timeToAdd);
   echo $total;
    ?>
于 2013-04-15T03:16:55.880 回答