我将这两个日期存储在 mysql 中:
2012-10-05
2012-10-10
我得到了使用 php/mysql 返回的日期,现在我需要找出将它们分开的天数。在本例中为 5 天。有什么建议最好的方法是什么?
SELECT DATEDIFF(date1, date2)
FROM yourtable
根据 MySQL 文档:http ://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
datediff() 返回date1 - date2
天数之差。
$date1 = strtotime("2012-10-05");
$date2 = strtotime("2012-10-10");
$days = floor(abs($date2 - $date1)/ (60*60*24));
printf("%d days",$days);
$date1 = new DateTime('2012-10-05');
$date2 = new Datetime('2012-10-10');
$interval = $date1->diff($date2);
echo $interval->format('%R%d days'); // +5 days
尝试这样的事情:
$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 年, %d 个月, %d 天\n", $years, $months, $days);