Calculate difference between 2 times in hours in PHP

后端 未结 7 696
無奈伤痛
無奈伤痛 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:42

    I think the following code is useful to get an idea about how to calculate time difference using PHP

    function date_diff($date_1 , $date_2 , $format) {
        $datetime1 = date_create($date_1);
        $datetime2 = date_create($date_2);
    
        $diff = date_diff($datetime1, $datetime2);
    
        return $diff->format($format);
    }
    

    The above function is useful to calculate difference between two times as well as dates. The dates are given as arguments with the output format.

    The output format are given below:

    // '%y Year %m Month %d Day %h Hours %i Minute %s Seconds' => 1 Year 3 Month 14 Day 11 Hours 49 Minute 36 Seconds // '%y Year %m Month %d Day' => 1 Year 3 Month 14 Days // '%m Month %d Day' => 3 Month 14 Day // '%d Day %h Hours' => 14 Day 11 Hours // '%d Day' => 14 Days // '%h Hours %i Minute %s Seconds' => 11 Hours 49 Minute 36 Seconds // '%i Minute %s Seconds' => 49 Minute 36 Seconds // '%h Hours => 11 Hours // '%a Days

提交回复
热议问题