在PHP中计算两个时间戳之间的差异。

26
我在我的表格中使用了2个时间戳,它们是:
starttime    datatype - timestamp and as current timestamp
endtime      datatype - timestamp and default as 0000-00-00 00:00:00

如何在PHP中计算两个时间戳之间的差异
starttime:   2016-11-30 03:55:06
endtime:     2016-11-30 11:55:06

1
你好,你想要什么间隔时间(秒、小时或天)? - Ronald
可能是重复的问题,参考如何在PHP中找到两个日期之间的小时差? - Ronald
2
可能是[如何在PHP中获取分钟时间差]的重复问题(https://dev59.com/ynRC5IYBdhLWcg3wP-dh)。 - chus
7个回答

41
应避免使用任何过程性的方法。
在日期时间差异方面,应使用面向对象编程(OOP)的方法。
$datetime1 = new DateTime('2016-11-30 03:55:06'); // start time
$datetime2 = new DateTime('2016-11-30 11:55:06'); // end time
$interval = $datetime1->diff($datetime2);
echo $interval
     ->format('%Y years %m months %d days %H hours %i minutes %s seconds');
     // 00 years 0 months 0 days 08 hours 0 minutes 0 seconds

您可以根据您的需要设置不同的格式。

%Y - use for difference in year
%m - use for difference in months
%d - use for difference in days
%H - use for difference in hours
%i - use for difference in minutes
%s - use for difference in seconds
您可以根据需要删除上述任何值。例如,如果您只对小时差感兴趣,并且知道差异不会超过24小时,则只使用%H
如果您想要以秒为单位的总差异,则可以使用:
echo $difference_in_seconds 
     = strtotime('2016-11-30 11:55:06') - strtotime('2016-11-30 03:55:06');
// 28800

根据您的需求和您希望获得时间差的最终格式而定。

请参考:https://www.php.net/manual/en/datetime.diff.php


4
为什么应该使用面向对象编程(OOP)而不是过程式编程(procedural)? - Derrick Miller
@DerrickMiller 总的来说,面向对象编程比过程式编程更好,原因有很多,这就是为什么我们应该尽可能地采用面向对象编程,如果可能的话,只使用面向对象编程。这里有一个链接,可能会帮助你理解为什么会这样 https://www.reddit.com/r/PHP/comments/7tzazd/procedural_vs_object_oriented_im_new_to_oop/ - Abhay Maurya

20
您可以使用PHP strtotime将时间戳转换为Unix时间戳(以秒为单位),然后取差值。您现在拥有以秒为单位的时间差,可以将其转换为需要的格式…小时、分钟、天等。 http://php.net/manual/en/function.strtotime.php 示例:
$ts1 = strtotime($start);
$ts2 = strtotime($end);     
$seconds_diff = $ts2 - $ts1;                            
$time = ($seconds_diff/3600);

2

我认为最简单的答案可以在这里找到。

function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
{
    $datetime1 = date_create($date_1);
    $datetime2 = date_create($date_2);

    $interval = date_diff($datetime1, $datetime2);

    return $interval->format($differenceFormat);

}

在这种情况下,date_create() 函数创建了 DateTime 对象。

1
我创建了一个函数,可以通过传递第三个参数来获取两个Unix时间戳之间的小时、分钟和天数。
public function diffBtwTimesAsPerType($start, $end, $returnType=1) {
    $seconds_diff = $start - $end;
    if($returnType == 1){
        return $seconds_diff/60;//minutes
    }else if($returnType == 2){
        return $seconds_diff/3600;//hours
    }else{
        return $seconds_diff/3600/24; //days
    }
}

echo "<br> Minutes = ".diffBtwTimesAsPerType(1593714600, 1593541800, 1);//minutes
echo "<br> Hours = ".diffBtwTimesAsPerType(1593714600, 1593541800, 2);//hours
echo "<br> Days = ".diffBtwTimesAsPerType(1593714600, 1593541800, 3);//days

0

@atul-baldaniya,我已经修改了你的解决方案,以避免负值并返回整数结果而不是小数值。

function diffBtwTimesAsPerType($start, $end, $returnType=1) {
    $seconds_diff = $end - $start;
    if($returnType == 1){
        return round($seconds_diff/60);//minutes
    }else if($returnType == 2){
        return round($seconds_diff/3600);//hours
    }else{
        return round($seconds_diff/3600/24); //days
    }
}

0
如果您需要以00:00:00(小时,分钟,秒)格式呈现差异,可以使用此函数。
我使用sprintf()函数,因为它在一位数字前添加零。
function timeDifference($start, $stop){
    
$diff = strtotime($stop) - strtotime($start);
$fullHours   = floor($diff/(60*60));
$fullMinutes = floor(($diff-($fullHours*60*60))/60);
$fullSeconds = floor($diff-($fullHours*60*60)-($fullMinutes*60));

return sprintf("%02d",$fullHours) . ":" . sprintf("%02d",$fullMinutes) . ":" . sprintf("%02d",$fullSeconds);
}

-1

尝试这段代码,在 phpfiddle.org 上测试过:

function timestampdiff($qw,$saw)
{
    $datetime1 = new DateTime("@$qw");
    $datetime2 = new DateTime("@$saw");
    $interval = $datetime1->diff($datetime2);
    return $interval->format('%Hh %Im');
}
echo timestampdiff('1524794340', '1524803100');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接