How to get time difference in minutes in PHP

后端 未结 17 2238
独厮守ぢ
独厮守ぢ 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;
    }
    
    0 讨论(0)
  • 2020-11-21 07:47

    Subtract the past most one from the future most one and divide by 60.

    Times are done in Unix format so they're just a big number showing the number of seconds from January 1, 1970, 00:00:00 GMT

    0 讨论(0)
  • 2020-11-21 07:48

    A more universal version that returns result in days, hours, minutes or seconds including fractions/decimals:

    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
    
    0 讨论(0)
  • 2020-11-21 07:50

    another way with timezone.

    $start_date = new DateTime("2013-12-24 06:00:00",new DateTimeZone('Pacific/Nauru'));
    $end_date = new DateTime("2013-12-24 06:45:00", new DateTimeZone('Pacific/Nauru'));
    $interval = $start_date->diff($end_date);
    $hours   = $interval->format('%h'); 
    $minutes = $interval->format('%i');
    echo  'Diff. in minutes is: '.($hours * 60 + $minutes);
    
    0 讨论(0)
  • 2020-11-21 07:51

    Here is the answer:

    $to_time = strtotime("2008-12-13 10:42:00");
    $from_time = strtotime("2008-12-13 10:21:00");
    echo round(abs($to_time - $from_time) / 60,2). " minute";
    
    0 讨论(0)
  • 2020-11-21 07:56
    <?php
    $date1 = time();
    sleep(2000);
    $date2 = time();
    $mins = ($date2 - $date1) / 60;
    echo $mins;
    ?>
    
    0 讨论(0)
提交回复
热议问题