Jump to content

I want to add x days to a datetime


shaggycap

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
Link to comment
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.
Link to comment
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);
[!--sql2--][/div][!--sql3--]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.