如何计算日期时间字段和现在在 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
How to calculate the difference of datetime field and now in PHP?
提问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));

