List: General Discussion « Previous MessageNext Message » From: Paul DuBois Date: March 2 2003 12:23am Subject: Re: Days in a month View as plain text
At 17:13 -0700 3/1/03, Steffan A. Cline wrote:
>Is there a function that I missed for calculating the number of days in a
>month?

No, but you can calculate it.  One way:
- Find the first of the month
- Subtract a day.  That gives you the last day of the month.
- Extract the day-of-the-month

Example:

SET @d = '2003-03-01';
SELECT @d,
DATE_SUB(
DATE_SUB(@d,INTERVAL DAYOFMONTH(@d) - 1 DAY),
INTERVAL 1 MONTH),
INTERVAL 1 DAY)
AS 'last of month';
SELECT @d,
DAYOFMONTH(
DATE_SUB(
DATE_SUB(@d,INTERVAL DAYOFMONTH(@d) - 1 DAY),
INTERVAL 1 MONTH),
INTERVAL 1 DAY)
)
AS 'days in month';

Which produces this output:

+------------+---------------+
| @d         | last of month |
+------------+---------------+
| 2003-03-01 | 2003-03-31    |
+------------+---------------+
+------------+---------------+
| @d         | days in month |
+------------+---------------+
| 2003-03-01 |            31 |
+------------+---------------+

Easy! :-)

There are other solutions.

>
>
>Steffan
>
>
>
>MySQL
>---------------------------------------------------------------
>T E L  6 0 2 . 5 7 9 . 4 2 3 0 | F A X  6 0 2 . 9 7 1 . 1 6 9 4
>Steffan A. Cline
>Steffan@stripped                             Phoenix, Az
>AIM : SteffanC          ICQ : 57234309
>The Executive's Choice in Lasso driven Internet Applications
>---------------------------------------------------------------