Ben bir tarih "Jan 31 2011" var ve ben bu tarihe kadar bir month eklemek istediğinizi varsayalım.
Yeni tarih tamam olduğunu ben bir ay eklemek istediğinizde "Feb 28 2011", yine bana "March 28 2011" verir, ama "March 31 2011" olması gerekir.
Oracle has a built-in function for that as Tony Andrews replied to this question,
Add date without exceeding a month.
Ama nasıl PHP bu işlevi uygulamak için? (Ayrıca, MySQL çözüm açığız)
edit #1
@zerkms, this is the nearest answer thanks for your help. Here is the second deal
Benim gerçek yaşam sorunu çözmek için çözüm biraz değişti
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?
edit #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)))
edit #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)