Jump to content


This topic is now archived and is closed to further replies.


I want to add x days to a datetime

Recommended Posts

I have a field in my DB called 'trial_start' which is a datetime with a default value of now().

There is another field in the DB called 'days' which is the no of days that the trial will run for.

I want another field, called 'trial_end' which is, you've guessed it, the 'trial_start' plus the number of days.

I am struggling as to what syntax to use, to calculate the 'trial_end' value. I assume I need to turn the days into seconds and add them onto the timestamp of the trial_start.

Can anyone help please as I'm struggling. Thanks

Share this post

Link to post
Share on other sites
My first question is why store all three? You only need two of them, and the 3rd can always be calculated. Second, how much sense does it make to add a fixed number of days to a DATETIME column -- doesn't that mean the time of the trial_end value is meaningless?

Anyway, regardless, you can always use INTERVAL to set it (UNTESTED):

[code]UPDATE yourTable SET trial_end = trial_start + INTERVAL days DAY WHERE....[/code]

Hope that helps.

Share this post

Link to post
Share on other sites
... or, just do them all at once:

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']UPDATE[/span] tableName SET trial_end [color=orange]=[/color] DATE_ADD(trial_start, INTERVAL days DAY);

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.