Search code examples
mysqldatediff

How many Intervals are in a period?


Given a table with two dates and a date interval (i.e. 6 WEEKS), how can I count how many times this interval fits in this date interval?

In my PHP script I'm using a simple for loop, but I'd like to add this in a MySQL query.

A simple calculation would be to count the number of days between the dates and then assume a month has 30 days and a year has 365 days, but that's not always true.

How can I calculate with datediff and a date interval string?


Solution

  • When I understood you correctly, you need the timestampdiff() function.

    The first parameter is the unit of the interval. The result is how many units are between the two dates.