Reputation: 4564
I have these 2 dates stored in mysql :
2012-10-05
2012-10-10
I got the dates returned using php/mysql now I need to find out how many days separate them. in this example it would be 5days. any suggestion of what would be the best way to do it ?
Upvotes: 0
Views: 2363
Reputation: 117
Try something like this :
$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);
Upvotes: 1
Reputation: 16462
$date1 = new DateTime('2012-10-05');
$date2 = new Datetime('2012-10-10');
$interval = $date1->diff($date2);
echo $interval->format('%R%d days'); // +5 days
Upvotes: 1
Reputation: 27835
$date1 = strtotime("2012-10-05");
$date2 = strtotime("2012-10-10");
$days = floor(abs($date2 - $date1)/ (60*60*24));
printf("%d days",$days);
Upvotes: 1
Reputation: 360572
SELECT DATEDIFF(date1, date2)
FROM yourtable
as per the MySQL docs: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
datediff() returns the difference of date1 - date2
in days.
Upvotes: 1