Date subtraction in days

A

Anonymous

Guest
How do I find the difference between two dates in days?

E.g. 08/01/2004 - 05/01/2004 = 3 days

The actual dates to be used will be in MySQL datetime format (yyyy-mm-dd), but I don't think that really matters for this question.

I'm sure this should be really simple, but I just can't figure it out! :(
 
If you're getting the dates from a MySQL database, then the easiest thing to do would be use MySQL's built-in date arithmetic functions. There's a couple sections on this in the documentation, here and here.
 
Thanks. I was thinking of using the MySQL functions, however I've just had a reply on another forum that works a treat:

Code:
function diff_days($start_date, $end_date){ 
   return floor(abs(strtotime($start_date) - strtotime($end_date))/86400); 
}
 
That would have been my second suggestion. :) By the way, if you're doing this on a number of MySQL records, it'll far faster to let MySQL do it as part of your query. But if you're just doing it once or twice, doing it in PHP will be just fine.
 
Back
Top