10

我的数据库中有一个日期时间字段,其中包含以下信息:

2012-05-03 17:34:01

我想检查日期时间字段和现在之间的区别:

$now = date("Y-m-d H:i:s");

我正在尝试计算从现在到写入数据库字段的时间已经过去了多少天。

我怎样才能做到这一点?

4

3 回答 3

34

这是答案:)

$now = new DateTime();
$date = new DateTime("2012-05-03 17:34:01");

echo $date->diff($now)->format("%d days, %h hours and %i minutes");
于 2012-05-04T11:57:21.473 回答
6
$diff = abs(strtotime($date2) - strtotime($date1));
于 2012-05-04T11:41:02.103 回答
4

日期差异

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime("now");
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
于 2012-05-04T11:40:58.730 回答