在PHP中添加时间结果不正确

时间:2013-04-15 02:28:30

标签: php time add

我有添加时间不正确的问题。 例: 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).' ';

我希望everybode = y在这里可以帮助我..

提前致谢..

更新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)));

我想知道这怎么可能发生?

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

1 个答案:

答案 0 :(得分: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;
    ?>