Agregar meses a una fecha manteniendo el último día del mes dado

Suppose that I have a date "Jan 31 2011" and I want to add a mes para esta fecha.

New date is is okay "Feb 28 2011", when I want to add a month again it gives me "March 28 2011", but I need it to be "March 31 2011".

Oracle has a built-in function for that as Tony Andrews replied to this question,
Add date without exceeding a month

But how to implement this function in PHP? (also MySQL solution is welcome)

editar # 1
@zerkms, this is the nearest answer thanks for your help. Here is the second deal

I changed your solution a little bit to solve my real-life problem

SET @BEGINNING_DATE := '2011-01-30'; /* first date for payment */  
SET @NEXT_END_DATE := '2011-02-28'; /* next date for payment (second payment deadline) */

/* now I want to find third payment deadline */

SELECT IF(@BEGINNING_DATE = LAST_DAY(@BEGINNING_DATE), LAST_DAY(DATE_ADD(@NEXT_END_DATE, INTERVAL 1 MONTH)), DATE_ADD(@NEXT_END_DATE, INTERVAL 1 MONTH))

Your helpful solution gives me "2011-03-28" but I want to "2011-03-30". Any ideas?

editar # 2
Here is the solution with @zerkms' help. Thank you!

SET @BEGINNING_DATE := '2011-02-28'; /* first date for payment */
SET @NEXT_END_DATE := '2011-05-31'; /* next date for payment (second payment deadline */

SELECT IF(@BEGINNING_DATE = LAST_DAY(@BEGINNING_DATE), LAST_DAY(DATE_ADD(@NEXT_END_DATE, INTERVAL 1 MONTH)), CONCAT(YEAR(DATE_ADD(@NEXT_END_DATE, INTERVAL 1 MONTH)),'-',LPAD(MONTH(DATE_ADD(@NEXT_END_DATE, INTERVAL 1 MONTH)),2,'0'),'-',DAY(@BEGINNING_DATE)))

editar # 3 Here is the exact solution. After dealing with some strange behaviours this is the exact solution

SET @@session.sql_mode = 'ALLOW_INVALID_DATES';
SET @BEGINNING_DATE := '2011-01-29'; /* first date for payment */  
SET @NEXT_END_DATE := '2011-02-28'; /* next date for payment (second payment deadline) */

SELECT IF(@BEGINNING_DATE = LAST_DAY(@BEGINNING_DATE), LAST_DAY(DATE_ADD(@NEXT_END_DATE, INTERVAL 1 MONTH)), CONCAT(YEAR(DATE_ADD(@NEXT_END_DATE, INTERVAL 1 MONTH)),'-',MONTH(DATE_ADD(@NEXT_END_DATE, INTERVAL 1 MONTH)),'-',DAY(@BEGINNING_DATE)) + INTERVAL 0 MONTH)

preguntado el 09 de enero de 11 a las 09:01

In mysql you can do this DATE_ADD('2006-05-00',INTERVAL 1 MONTH) to add a month to a specified date -

Bueno, 27 feb 2011 + month == 27 march 2011. 28 feb 2011 + month == 31 march 2011. Is not it strange? What task are you trying to solve in this way? -

if you want the last day of next month look on stackoverflow.com/questions/3009896/… -

@Octopus-Paul: date_add behaves not the way OP expected. -

@zerkms: I need this to calculate the last payment date for customers. -

2 Respuestas

SET @DT := '2011-02-28';

SELECT IF(@DT = LAST_DAY(@DT), LAST_DAY(DATE_ADD(@DT, INTERVAL 1 MONTH)), DATE_ADD(@DT, INTERVAL 1 MONTH));

// returns 2011-03-31

This query takes into account if it is the end of the month or not.

Respondido el 09 de enero de 11 a las 12:01

MySql has a ÚLTIMO DÍA function which returns the last day of any given month.

Respondido el 09 de enero de 11 a las 12:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.