如何在php中找到datetime类型与当前日期/时间之间的区别? [英] How do I find the difference between a datetime type and the current date/time in php?
问题描述
我有一个即将到来的数据库中的条目的DATETIME。我想知道DATETIME与当前日期/时间之间的差异,以天,小时,分钟和秒为单位。我以为我可以使用date函数来做到这一点,但也许我错了。
I have a DATETIME for an entry in my database that is upcoming. I want to know the difference in time between the the DATETIME and the current date/time in Days, Hours, Minutes, and Seconds. I thought I might be able to use the date function to do this, but perhaps I am wrong.
这是我的方法:
$now = mktime(0, 0, 0, date("m"), date("d"), date("y"));
$entry_datetime = strtotime($row['end_auction']);
$difference = date("G, i, s",($entry_datetime - $now));
echo $difference;
我收到的结果是
13,20,00
13, 20, 00
但这并没有任何意义,因为$ row [' end_auction']是11月28日,今天是11月19日,间隔超过13小时。
but that doesn't make any sense since the DATETIME in $row['end_auction'] is November 28th and today is November 19th, so much more than 13 hours apart.
如何找到两个值之间的区别,如何格式化它显示为:
How do I find the difference between the two values and how do I format it to appear as:
9天,10小时,32分钟和20
秒
9 days, 10 hours, 32 minutes, and 20 seconds
提前谢谢!
推荐答案
function datediff( $date1, $date2 )
{
$diff = abs( strtotime( $date1 ) - strtotime( $date2 ) );
return sprintf
(
"%d Days, %d Hours, %d Mins, %d Seconds",
intval( $diff / 86400 ),
intval( ( $diff % 86400 ) / 3600),
intval( ( $diff / 60 ) % 60 ),
intval( $diff % 60 )
);
}
print datediff( "24th November 2009", "now" ) . "\n";
这篇关于如何在php中找到datetime类型与当前日期/时间之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!