php 如何在PHP中以分钟为单位获得时差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/365191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:30:36  来源:igfitidea点击:

How to get time difference in minutes in PHP

phpdatetimeminute

提问by Tomasz Smykowski

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

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

采纳答案by Oli

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

将过去最多的 1 减去未来最多的 1 再除以 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

时间以 Unix 格式完成,因此它们只是一个大数字,显示从 January 1, 1970, 00:00:00 GMT

回答by mike

The answers above are for older versions of PHP. Use the DateTime class to do any date calculations now that PHP 5.3 is the norm. Eg.

上面的答案适用于旧版本的 PHP。既然 PHP 5.3 是标准,那么使用 DateTime 类进行任何日期计算。例如。

$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 is a DateIntervalobject. Note that the days property is available (because we used the diff method of the DateTime class to generate the DateInterval object).

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

The above code will output:

上面的代码会输出:

1837 days total
5 years
0 months
10 days
6 hours
14 minutes
2 seconds

共 1837 天
5 年
0 月
10 天
6 小时
14 分
2 秒

To get the total number of minutes:

获取总分钟数:

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

This will output:

这将输出:

2645654 minutes

2645654 分钟

Which is the actual number of minutes that has passed between the two dates. The DateTime class will take daylight saving (depending on timezone) into account where the "old way" won't. Read the manual about Date and Time http://www.php.net/manual/en/book.datetime.php

这是两个日期之间经过的实际分钟数。DateTime 类将考虑夏令时(取决于时区),而“旧方式”不会。阅读有关日期和时间的手册http://www.php.net/manual/en/book.datetime.php

回答by user38526

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

回答by Tom

<?php
$date1 = time();
sleep(2000);
$date2 = time();
$mins = ($date2 - $date1) / 60;
echo $mins;
?>

回答by yussan

It worked on my programs, i'am using date_diff, you can check date_diffmanual on here.

它适用于我的程序,我正在使用date_diff,您可以date_diff此处查看手册。

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

You get results what do you want.

你得到你想要的结果。

回答by Muhammad

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

回答by Raj Sharma

I wrote this function for one my blog site(difference between a past date and server's date). It will give you an output like this

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

"49 seconds ago", "20 minutes ago", "21 hours ago" and so on

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

I have used a function that would get me the difference between the date passed and the server's date.

我使用了一个函数,它可以让我知道传递的日期和服务器的日期之间的差异。

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

}
?>

Save it as a file suppose "date.php". Call the function from another page like this

将它保存为一个文件,假设为“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);
?>

Of course you can modify the function to pass two values.

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

回答by Yubraj Pokharel

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

回答by Veerendra

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

回答by besimple

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