How to get the number of days of a month based on a specific month and year?

I am new to sql and programming, so please bear with me. Is there a function in mysql that gets the number of days per month? Example, if the month is February and 2012, it should return 29. Thanks in advance.

+5
source share
1 answer

There is no direct “last day of the month”, but you can fake it by doing the day (last_day ())

mysql> select last_day('2012-02-22'), day(last_day('2012-02-22'));
+------------------------+-----------------------------+
| last_day('2012-02-22') | day(last_day('2012-02-22')) |
+------------------------+-----------------------------+
| 2012-02-29             |                          29 | 
+------------------------+-----------------------------+
1 row in set (0.00 sec)

last_day()returns the full date of the last day in the year / month of the specified date, so you simply use day()this last_day to retrieve the day.

The documentation for MySQL date and time functions is given here: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

+12

All Articles