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 ?
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.
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);
strtotime()accepts a string and returns an a unix timestamp. If you didstrtotime()for each and subtracted, you'd have the difference between the two dates in seconds. Divide appropriately and you're on your way.