I need to subtract 1 month and 4 days with mysql, I saw the command
DATE_ADD (NOW (), - 1 MONTH) perfect for 1 month but for 1 month and 4 days, using 31 days is not valid for every month that some bring 30, 29, 28. I can not add 31 + 4, 30 + 4, etc.
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
DATE_SUB((DATE_SUB(curdate(), INTERVAL 1 MONTH)), INTERVAL 4 DAY)
SELECT DATE_ADD(DATE_ADD(NOW(),INTERVAL -1 MONTH), INTERVAL -4 DAY)
Keep it simple:
SELECT CURDATE() - INTERVAL 1 MONTH - INTERVAL 4 DAY;
SELECT '2014-03-27' - INTERVAL 1 MONTH - INTERVAL 4 DAY;
or if you like to preserve the current time:
SELECT NOW() - INTERVAL 1 MONTH - INTERVAL 4 DAY;
(Tested on MySQL 5.1.73 and newer)
SELECT CURRENT_TIMESTAMP + INTERVAL - 1 MONTH + INTERVAL - 4 DAY;
SELECT CURRENT_DATE + INTERVAL - 1 MONTH + INTERVAL - 4 DAY;