How to get time difference in minutes in PHP

后端 未结 17 2301
独厮守ぢ
独厮守ぢ 2020-11-21 07:22

How to calculate minute difference between two date-times in PHP?

17条回答
  •  时光取名叫无心
    2020-11-21 08:05

    My solution to find the difference between two dates is here. With this function you can find differences like seconds, minutes, hours, days, years and months.

    function alihan_diff_dates($date = null, $diff = "minutes") {
     $start_date = new DateTime($date);
     $since_start = $start_date->diff(new DateTime( date('Y-m-d H:i:s') )); // date now
     print_r($since_start);
     switch ($diff) {
        case 'seconds':
            return $since_start->s;
            break;
        case 'minutes':
            return $since_start->i;
            break;
        case 'hours':
            return $since_start->h;
            break;
        case 'days':
            return $since_start->d;
            break;      
        default:
            # code...
            break;
     }
    }
    

    You can develop this function. I tested and works for me. DateInterval object output is here:

    /*
    DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 0 [i] => 5 [s] => 13 [f] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => 0 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 ) 
    */
    

    Function Usage:

    $date = the past date, $diff = type eg: "minutes", "days", "seconds"

    $diff_mins = alihan_diff_dates("2019-03-24 13:24:19", "minutes");
    

    Good Luck.

提交回复
热议问题