Calculate difference between 2 times in hours in PHP

后端 未结 7 723
無奈伤痛
無奈伤痛 2020-12-10 20:11

I have two times - For eg- the current time - 08:24 and date is 02/01/2013 in dd/mm/yyyy format and I have another time at 13:46 and date is 31/12/2012 . So, how can I cal

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-10 20:20

    I got a simple solution, Try this one -

    echo getTimeDiff("10:30","11:10");
    
    function getTimeDiff($dtime,$atime)
        {
            $nextDay = $dtime>$atime?1:0;
            $dep = explode(':',$dtime);
            $arr = explode(':',$atime);
            $diff = abs(mktime($dep[0],$dep[1],0,date('n'),date('j'),date('y'))-mktime($arr[0],$arr[1],0,date('n'),date('j')+$nextDay,date('y')));
            $hours = floor($diff/(60*60));
            $mins = floor(($diff-($hours*60*60))/(60));
            $secs = floor(($diff-(($hours*60*60)+($mins*60))));
            if(strlen($hours)<2){$hours="0".$hours;}
            if(strlen($mins)<2){$mins="0".$mins;}
            if(strlen($secs)<2){$secs="0".$secs;}
            return $hours.':'.$mins.':'.$secs;
        }
    

提交回复
热议问题