shaggycap Posted February 10, 2006 Share Posted February 10, 2006 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 https://forums.phpfreaks.com/topic/3379-i-want-to-add-x-days-to-a-datetime/ Share on other sites More sharing options...
fenway Posted February 10, 2006 Share Posted February 10, 2006 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 https://forums.phpfreaks.com/topic/3379-i-want-to-add-x-days-to-a-datetime/#findComment-11553 Share on other sites More sharing options...
obsidian Posted February 10, 2006 Share Posted February 10, 2006 ... 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 https://forums.phpfreaks.com/topic/3379-i-want-to-add-x-days-to-a-datetime/#findComment-11556 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.