如何在PHP计算两个日期时间之间微小差异?

有帮助吗?

解决方案

从未来最之一并且通过60.除以减去过去最多一个

时间以UNIX格式这样做,他们只是表示从January 1, 1970, 00:00:00 GMT秒数一个大数目

其他提示

上面的答案是旧版本的PHP。使用DateTime类现在PHP 5.3是规范做任何日期计算。 例如

$start_date = new DateTime('2007-09-01 04:10:58');
$since_start = $start_date->diff(new DateTime('2012-09-11 10:25:00'));
echo $since_start->days.' days total<br>';
echo $since_start->y.' years<br>';
echo $since_start->m.' months<br>';
echo $since_start->d.' days<br>';
echo $since_start->h.' hours<br>';
echo $since_start->i.' minutes<br>';
echo $since_start->s.' seconds<br>';

$ since_start是 DateInterval 对象。请注意,天属性是可用的(因为我们使用DateTime类的DIFF方法来生成DateInterval对象)。

在上面的代码将输出:

1837天总结果5年结果0个月结果10天结果6小时结果14分钟结果2秒结果

要得到分钟的总数量:

$minutes = $since_start->days * 24 * 60;
$minutes += $since_start->h * 60;
$minutes += $since_start->i;
echo $minutes.' minutes';

此将输出:

2645654分钟

其是具有两个日期之间传递分钟的实际数量。 DateTime类将采取夏令(根据时区),到户口所在的“老办法”不会。阅读关于日期和时间 http://www.php.net/manual/en/book.datetime手册.PHP

下面是答案:

$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";
<?php
$date1 = time();
sleep(2000);
$date2 = time();
$mins = ($date2 - $date1) / 60;
echo $mins;
?>

它的工作在我的节目,使用date_diff我',你可以检查的date_diff手册的rel =“noreferrer “>此处

$start = date_create('2015-01-26 12:01:00');
$end = date_create('2015-01-26 13:15:00');
$diff=date_diff($end,$start);
print_r($diff);

您得到的结果,你想要什么。

与时区的另一种方式。

$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);

我想这会帮助你。

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

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

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

我用这将让我通过的日期和服务器的日期之间的差异的功能。

<?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);
?>

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

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

这天,小时,分或秒,包括馏分/小数返回结果的更普遍的版本:

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

这是我如何在PHP> 5.2显示 “XX时代前” ..这里是的日期时间对象

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

我的解决办法找到两个日期之间的区别就在这里。有了这个功能,你可以找到像秒,分钟,小时,天,年和月的差异。

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

您可以开发这个功能。我测试,对我的作品。 DateInterval对象输出是在这里:

/*
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 ) 
*/

功能用法:

$日期=过去日期, $ DIFF =类型例如: “分钟”, “天”, “秒”

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

好运。

这将有助于....

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

我发现这么多的解决方案,但我从来没有得到正确的解决方案。但我已经创造了一些代码,以查找分钟,请检查一下。

<?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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top