How to Get the Number of Days in a Month in MySQL

Check out the following technique in MySQL if you need to find out how many days are in a month based on a given date.

Example

The easiest way to explain it is with an example:

SELECT DAYOFMONTH(LAST_DAY('2030-04-15'));

Result:

30

Here, we passed the LAST_DAY() function to the DAYOFMONTH() function.

So we passed the date to the LAST_DAY() function in order to get the date of the last day of the month. We then used the DAYOFMONTH() function to return just the day number of that last day.

Here’s an example that goes through all months of the year:

SELECT 
    DAYOFMONTH(LAST_DAY('2030-01-15')) AS "Jan",
    DAYOFMONTH(LAST_DAY('2030-02-15')) AS "Feb",
    DAYOFMONTH(LAST_DAY('2030-03-15')) AS "Mar",
    DAYOFMONTH(LAST_DAY('2030-04-15')) AS "Apr",
    DAYOFMONTH(LAST_DAY('2030-05-15')) AS "May",
    DAYOFMONTH(LAST_DAY('2030-06-15')) AS "Jun",
    DAYOFMONTH(LAST_DAY('2030-07-15')) AS "Jul",
    DAYOFMONTH(LAST_DAY('2030-08-15')) AS "Aug",
    DAYOFMONTH(LAST_DAY('2030-09-15')) AS "Sep",
    DAYOFMONTH(LAST_DAY('2030-10-15')) AS "Oct",
    DAYOFMONTH(LAST_DAY('2030-11-15')) AS "Nov",
    DAYOFMONTH(LAST_DAY('2030-12-15')) AS "Dec";

Result:

Jan	Feb	Mar	Apr	May	Jun	Jul	Aug	Sep	Oct	Nov	Dec
31	28	31	30	31	30	31	31	30	31	30	31