如何在PHP中计算两个日期时间之间的分钟差异?


当前回答

function date_getFullTimeDifference( $start, $end )
{
$uts['start']      =    strtotime( $start );
        $uts['end']        =    strtotime( $end );
        if( $uts['start']!==-1 && $uts['end']!==-1 )
        {
            if( $uts['end'] >= $uts['start'] )
            {
                $diff    =    $uts['end'] - $uts['start'];
                if( $years=intval((floor($diff/31104000))) )
                    $diff = $diff % 31104000;
                if( $months=intval((floor($diff/2592000))) )
                    $diff = $diff % 2592000;
                if( $days=intval((floor($diff/86400))) )
                    $diff = $diff % 86400;
                if( $hours=intval((floor($diff/3600))) )
                    $diff = $diff % 3600;
                if( $minutes=intval((floor($diff/60))) )
                    $diff = $diff % 60;
                $diff    =    intval( $diff );
                return( array('years'=>$years,'months'=>$months,'days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) );
            }
            else
            {
                echo "Ending date/time is earlier than the start date/time";
            }
        }
        else
        {
            echo "Invalid date/time data detected";
        }
}

其他回答

我找到了很多解,但我从来没有得到正确的解。但是我已经创建了一些代码来查找分钟,请检查它。

<?php

  $time1 = "23:58";
  $time2 = "01:00";
  $time1 = explode(':',$time1);
  $time2 = explode(':',$time2);
  $hours1 = $time1[0];
  $hours2 = $time2[0];
  $mins1 = $time1[1];
  $mins2 = $time2[1];
  $hours = $hours2 - $hours1;
  $mins = 0;
  if($hours < 0)
  {
    $hours = 24 + $hours;
  }
  if($mins2 >= $mins1) {
        $mins = $mins2 - $mins1;
    }
    else {
      $mins = ($mins2 + 60) - $mins1;
      $hours--;
    }
    if($mins < 9)
    {
      $mins = str_pad($mins, 2, '0', STR_PAD_LEFT);
    }
    if($hours < 9)
    {
      $hours =str_pad($hours, 2, '0', STR_PAD_LEFT);
    }
echo $hours.':'.$mins;
?>

它以小时和分钟为单位输出,例如01小时02分钟,如01:02

<?php
$start = strtotime('12:01:00');
$end = strtotime('13:16:00');
$mins = ($end - $start) / 60;
echo $mins;
?>

输出:

75

我为我的博客网站写了这个函数(过去的日期和服务器的日期之间的差异)。它会给你一个这样的输出

“49秒前”,“20分钟前”,“21小时前”等等

我使用了一个函数来获取传递的日期和服务器日期之间的差值。

<?php

//Code written by purpledesign.in Jan 2014
function dateDiff($date)
{
    $mydate= date("Y-m-d H:i:s");
    $theDiff="";
    //echo $mydate;//2014-06-06 21:35:55
    $datetime1 = date_create($date);
    $datetime2 = date_create($mydate);
    $interval = date_diff($datetime1, $datetime2);
    //echo $interval->format('%s Seconds %i Minutes %h Hours %d days %m Months %y Year    Ago')."<br>";
    $min=$interval->format('%i');
    $sec=$interval->format('%s');
    $hour=$interval->format('%h');
    $mon=$interval->format('%m');
    $day=$interval->format('%d');
    $year=$interval->format('%y');
    if($interval->format('%i%h%d%m%y')=="00000") {
        //echo $interval->format('%i%h%d%m%y')."<br>";
        return $sec." Seconds";
    } else if($interval->format('%h%d%m%y')=="0000"){
        return $min." Minutes";
    } else if($interval->format('%d%m%y')=="000"){
        return $hour." Hours";
    } else if($interval->format('%m%y')=="00"){
        return $day." Days";
    } else if($interval->format('%y')=="0"){
        return $mon." Months";
    } else{
        return $year." Years";
    }    
}
?>

以date.php为例,将其保存为文件。像这样从另一个页面调用函数

<?php
 require('date.php');
 $mydate='2014-11-14 21:35:55';
 echo "The Difference between the server's date and $mydate is:<br> ";
 echo dateDiff($mydate);
?>

当然,您可以修改函数来传递两个值。

这就是我如何显示“xx次前”在php > 5.2 ..这里是DateTime对象的更多信息

//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;
}

减去这些次数,再除以60。

下面是一个计算从2019/02/01 10:23:45经过的时间(以分钟为单位)的例子:

$diff_time=(strtotime(date("Y/m/d H:i:s"))-strtotime("2019/02/01 10:23:45"))/60;