How to get time difference in minutes in PHP

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

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

17条回答
  •  星月不相逢
    2020-11-21 07:46

    I think this will help you

    function calculate_time_span($date){
        $seconds  = strtotime(date('Y-m-d H:i:s')) - strtotime($date);
    
            $months = floor($seconds / (3600*24*30));
            $day = floor($seconds / (3600*24));
            $hours = floor($seconds / 3600);
            $mins = floor(($seconds - ($hours*3600)) / 60);
            $secs = floor($seconds % 60);
    
            if($seconds < 60)
                $time = $secs." seconds ago";
            else if($seconds < 60*60 )
                $time = $mins." min ago";
            else if($seconds < 24*60*60)
                $time = $hours." hours ago";
            else if($seconds < 24*60*60)
                $time = $day." day ago";
            else
                $time = $months." month ago";
    
            return $time;
    }
    

提交回复
热议问题