This weekend I needed a couple of MySQL query items that were out of the ordinary, so I thought I would write about them.
ADDDATE or DATE_ADD both work the same, but came in very handy. I was struggling with an application where I need to have the date 42 days from now. PHP doesn’t really have anything that is easy to use, and after a few tries I decided to turn to MySQL. Below is what MySQL.com has to say on these:
When invoked with the INTERVAL form of the second argument, ADDDATE() is a synonym for DATE_ADD(). The related function SUBDATE() is a synonym for DATE_SUB(). For information on the INTERVAL unit argument, see the discussion for DATE_ADD().
SELECT DATE_ADD('1998-01-02', INTERVAL 42 DAY); -> '1998-02-02' SELECT ADDDATE('1998-01-02', INTERVAL 42 DAY); -> '1998-02-02'
3 responses to “MySQL ADDDATE or DATE_ADD”
There are 6 types of INTERVAL in ADDDATE or DATE_ADD. Those are: SECOND, MINUTE, HOUR, DAY, MONTH, YEAR
So You could use:
mysql> SELECT DATE_ADD('1998-01-02', INTERVAL 1 MONTH);
I chose the following:
SELECT ADDDATE( ‘2009-01-02’, INTERVAL 10
Got the answer – 2009-01-12
Question is how to populate my datefield from 2009-01-02 all
the way to 2009-01-12
I am a little confused by your question. More clarification please.
What would the desired ending value of datefield be?
What starting data would you have in order to request the ending value desired?