How to get time difference in minutes in PHP

2018-12-31 02:09发布

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

12条回答
倾城一夜雪
2楼-- · 2018-12-31 02:29

This is how I displayed "xx times ago" in php > 5.2 .. here is more info on DateTime object

//Usage:
$pubDate = $row['rssfeed']['pubDates']; // e.g. this could be like 'Sun, 10 Nov 2013 14:26:00 GMT'
$diff = ago($pubDate);    // output: 23 hrs ago

// Return the value of time different in "xx times ago" format
function ago($timestamp)
{

$today = new DateTime(date('y-m-d h:i:s')); // [2]
//$thatDay = new DateTime('Sun, 10 Nov 2013 14:26:00 GMT');
$thatDay = new DateTime($timestamp);
$dt = $today->diff($thatDay);

if ($dt->y > 0)
{
    $number = $dt->y;
    $unit = "year";
}
else if ($dt->m > 0)
{
    $number = $dt->m;
    $unit = "month";
}   
else if ($dt->d > 0)
{
    $number = $dt->d;
   $unit = "day";
}
else if ($dt->h > 0)
{
    $number = $dt->h;
    $unit = "hour";
}
else if ($dt->i > 0)
{
    $number = $dt->i;
    $unit = "minute";
}
else if ($dt->s > 0)
{
    $number = $dt->s;
    $unit = "second";
}

$unit .= $number  > 1 ? "s" : "";

$ret = $number." ".$unit." "."ago";
return $ret;
}
查看更多
长期被迫恋爱
3楼-- · 2018-12-31 02:34

This will help....

function get_time($date,$nosuffix=''){
    $datetime = new DateTime($date);
    $interval = date_create('now')->diff( $datetime );
    if(empty($nosuffix))$suffix = ( $interval->invert ? ' ago' : '' );
    else $suffix='';
    //return $interval->y;
    if($interval->y >=1)        {$count = date(VDATE, strtotime($date)); $text = '';}
    elseif($interval->m >=1)    {$count = date('M d', strtotime($date)); $text = '';}
    elseif($interval->d >=1)    {$count = $interval->d; $text = 'day';} 
    elseif($interval->h >=1)    {$count = $interval->h; $text = 'hour';}
    elseif($interval->i >=1)    {$count = $interval->i; $text = 'minute';}
    elseif($interval->s ==0)    {$count = 'Just Now'; $text = '';}
    else                        {$count = $interval->s; $text = 'second';}
    if(empty($text)) return '<i class="fa fa-clock-o"></i> '.$count;
    return '<i class="fa fa-clock-o"></i> '.$count.(($count ==1)?(" $text"):(" ${text}s")).' '.$suffix;     
}
查看更多
高级女魔头
4楼-- · 2018-12-31 02:36
<?php
$date1 = time();
sleep(2000);
$date2 = time();
$mins = ($date2 - $date1) / 60;
echo $mins;
?>
查看更多
刘海飞了
5楼-- · 2018-12-31 02:37

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;
}
查看更多
只靠听说
6楼-- · 2018-12-31 02:41

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";
查看更多
忆尘夕之涩
7楼-- · 2018-12-31 02:42

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);
查看更多
登录 后发表回答