Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Adding Days To Dates In MySQL

7 Responses »

One of my customers recently asked me to increment the date field on an event in their database by 14 days because their upcoming event got delayed. I knew the SQL statement would be simple and after doing some quick research, I found the magical SQL:

UPDATE events SET date_starts = DATE_ADD(date_starts,INTERVAL 14 DAY) WHERE event_id = 3;

This works on DATE and DATETIME field types. Note that it's "14 DAY" and not "14 DAYS".

Discussion

  1. April 25, 2008 @ 9:01 am

    Nice but I use the time() function, what is better ?

  2. April 25, 2008 @ 10:14 am

    @Catar: I don’t know which one is better, per say, but I know that this worked wonderfully!

  3. felix
    April 28, 2008 @ 9:38 pm

    it’s good!!!!…works fine and it’s usefull for me. tks!!

  4. May 17, 2008 @ 1:11 pm

    Nice! Wouldn’t have occurred to me that MySQL had such a feature. Personally I prefer to do all my data arithmetic in seconds before handing the result over to an SQL query, but this is worth noting.

  5. November 2, 2009 @ 8:23 am

    Your blog is my Quick Bible in the Caos of Days

  6. red
    February 10, 2010 @ 9:01 pm

    hi,

    i’m new to sql and i’m using mysql workbench to generate data mart. i need help..i want to generate dates (e.g. 2005-01-01), day of week ( 1 to 7), day of month (1 to 31), day name and month name for the whole of year 2005. the primary key is the date_key which starts from 1 to 365 and i already upload the date key into my database. it just that i need to generate all the above attributes using update/set but i dont know what is the correct sql script to generate them automatically and accordingly. would appreciate if u could help me on this..thanks

  7. saleem tahir
    April 12, 2010 @ 9:18 am

    Ok- That is helpfull but I want something like

    ” In a form an event is being added and it is add date is set to system date (today) Now I want the Event Expiry date to be automatically added in the event Expiry Date column based on the user choice. i.e User is give the choice to select that the event will expire in Two weeks, four weeks or Six weeks from the date of Adding”

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!