There is no need to put that burden on PHP when MySQL has built-in functionality for that already. You should take a look at MySQL's DATEDIFF() function:
DATEDIFF()
returns expr1
– expr2
expressed as a value in days from one date to the other. expr1
and expr2
are date or date-and-time expressions. Only the date parts of the values are used in the calculation.
An example of two dates that'd give a 7-day difference could be:
mysql> select datediff('2011-06-18','2011-06-25');
+-------------------------------------+
| datediff('2011-06-18','2011-06-25') |
+-------------------------------------+
| -7 |
+-------------------------------------+
This means that the first date occured -7 days after the first date; that's 7 days before. If you let the two arguments switch place, the result would be a positive 7.