如何找到两次之间的差异?

时间:2018-09-25 13:43:32

标签: php

我正在使用以下代码查找时差,但这给了我错误的时差。我关注了以下链接thisthis,但区别是错误的。

$diff = date_diff(date_create(date("Y-m-d h:i A", $this->start_time)), date_create(date("Y-m-d h:i A")));
echo '<span style = "color: #739e3b">'.$diff->d.' Day(s) : '.$diff->h.' Hr(s) : '.$diff->i.' Min(s)</span>';

2 个答案:

答案 0 :(得分:5)

程序:

<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>

这是OOP方式:

<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>

以下是文档:http://php.net/manual/en/datetime.diff.php 这是一个很棒的图书馆,可让您的生活更轻松:https://carbon.nesbot.com

答案 1 :(得分:0)

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