如何计算日期时间字段和现在在 PHP 中的差异?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10448119/
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:10:04  来源:igfitidea点击:

How to calculate the difference of datetime field and now in PHP?

phpdatetime

提问by hairynuggets

I have a datetime field in my database that contains the following information:

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

2012-05-03 17:34:01

I want to check the difference between the datetime field and now:

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

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

I am attempting to work out how many days have passed between now and the time written to the database field.

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

How can I achieve this?

我怎样才能做到这一点?

回答by Max M

Here is the answer :)

这是答案:)

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

echo $date->diff($now)->format("%d days, %h hours and %i minuts");

回答by swati

$diff = abs(strtotime($date2) - strtotime($date1));

回答by WojtekT

date_diff:

日期差异

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime("now");
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');