php中日期对象之间的差异
我需要找出php中两个日期对象之间的差异 我试过这个:php中日期对象之间的差异,php,datetime,Php,Datetime,我需要找出php中两个日期对象之间的差异 我试过这个: if(strtotime($current)>strtotime($LastUpdated)) { $diff=strtotime($current) - strtotime($LastUpdated); } else { $diff=strtotime($LastUpdated) - strtotime($current); } 这给了我垃圾价值观 我也试过这个 $diff=date_diff(new DateTi
if(strtotime($current)>strtotime($LastUpdated))
{
$diff=strtotime($current) - strtotime($LastUpdated);
}
else
{
$diff=strtotime($LastUpdated) - strtotime($current);
}
这给了我垃圾价值观
我也试过这个
$diff=date_diff(new DateTime($current),new DateTime($LastUpdated));
这给了我零分
我怎样才能找到区别呢?这本手册是你的朋友。- 以面向对象和过程编程为例 从上面的链接粘贴: 面向对象: 程序性:
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
结果将是:
+2 days
玩得开心:)
你得到了什么样的垃圾价值观?在第一个代码中,$diff
应该是以秒为单位的差值。什么类型的对象是$current
和$LastUpdated
?您的问题显示日期对象,但strotime()
需要一个字符串。$current=date('Y-m-dh:i:s',time());谢谢@Barmar。该值确实以秒为单位。没有意识到@你应该看看链接。你有需要的例子来理解你需要的解决方案,以及两个日期之间的差异是如何工作的。我不在乎链接中有什么。这可能是对他们问题的完美回答,回答SO>上的问题仍然不是一种可以接受的方式。如果OP有能力从文档中的示例中找出问题,他就不会问这样的问题。@Barmar所以你认为OP不称职,你也没有给他足够的工具让他变得称职。你只要把答案扔给他们就行了你把鱼给了他,而不是教他怎么钓鱼。你两者都可以。解释如何解决他的问题,并向他指出提供更多细节的文档。
+2 days
<?php
$date1 = "2007-03-24";
$date2 = "2009-06-26";
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
printf("%d years, %d months, %d days\n", $years, $months, $days);