PHP convert date interval diff to decimal

后端 未结 3 773
一向
一向 2021-01-20 14:51

I\'m trying to convert the difference between two dates into a total year count, right now I\'m using this:

 $datetime1 = new DateTime(\'2009-10-11\'); 
 $da         


        
3条回答
  •  暖寄归人
    2021-01-20 15:34

    Simpler and more accurate interval converter to days/hours/minutes/seconds:

    function DateDiffInterval($sDate1, $sDate2, $sUnit='H') {
    //subtract $sDate2-$sDate1 and return the difference in $sUnit (Days,Hours,Minutes,Seconds)
        $nInterval = strtotime($sDate2) - strtotime($sDate1);
        if ($sUnit=='D') { // days
            $nInterval = $nInterval/60/60/24;
        } else if ($sUnit=='H') { // hours
            $nInterval = $nInterval/60/60;
        } else if ($sUnit=='M') { // minutes
            $nInterval = $nInterval/60;
        } else if ($sUnit=='S') { // seconds
        }
        return $nInterval;
    } //DateDiffInterval
    

提交回复
热议问题