LAST_DAY( )

This MySQL function returns the date of the last day of the month for a given date or datetime value.

Syntax

Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

LAST_DAY(date)

Explanation

This function returns the date of the last day of the month for a given date or datetime value. NULL is returned for invalid dates. It's available as of version 4.1.1 of MySQL.

Examples

SELECT LAST_DAY('2008-12-15')
AS 'End of Month';

+--------------+
| End of Month |
+--------------+
| 2008-12-31   |
+--------------+

There is no FIRST_DAY( ) function at this time. However, you can use LAST_DAY( ) in conjunction with a couple of other functions to return the first day of the month.

SELECT CURDATE( ) AS 'Today',
ADDDATE(LAST_DAY(SUBDATE(CURDATE(), INTERVAL 1 MONTH)), 1)
AS 'First Day of Month';

+------------+--------------------+
| Today      | First Day of Month |
+------------+--------------------+
| 2008-06-18 | 2008-06-01         | 
+------------+--------------------+

In this example, we are subtracting one month from the results of CURDATE( ) to get the same day last month. From there, we're using LAST_DAY( ) to find the last day of last month. Then ADDDATE( ) is employed to add one day to the results, to find the first day of the month after last month, that is to say, the current month. This method adjusts for dates in January which would involve a previous year.