我将这两个日期存储在mysql中:
2012-10-05
2012-10-10
我现在使用php / mysql返回日期我需要找出分隔它们的天数。 在这个例子中它将是5天。 什么是最好的方法呢?
答案 0 :(得分:1)
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
的差异。
答案 1 :(得分:1)
$date1 = strtotime("2012-10-05");
$date2 = strtotime("2012-10-10");
$days = floor(abs($date2 - $date1)/ (60*60*24));
printf("%d days",$days);
答案 2 :(得分:1)
$date1 = new DateTime('2012-10-05');
$date2 = new Datetime('2012-10-10');
$interval = $date1->diff($date2);
echo $interval->format('%R%d days'); // +5 days
答案 3 :(得分:1)
尝试这样的事情:
$ 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”,$年,$月,$天);