How to Calculate Date Ranges in MySQL
- 1). Log in to your MySQL database and access the query tool.
- 2). Enter the following query:
SELECT DATEDIFF('2011-04-30 12:34:56','2011-01-01');
Execute this query. Note that the first expression has a time stamp included. The DATEDIFF function omits time stamps and returns only a date range. This example returns "119," the number of days between Jan. 1, 2011 and April 30, 2011. - 3). Enter the following query:
SELECT DATEDIFF(now(),'2011-01-01');
Execute this query. The "now()" function returns the current date and time. Again, MySQL ignores time, so it only calculates the range between the current day and Jan. 1, 2011. Do not put quotation marks around the "now()" expression in the query.
Source...