Jump to content

Adding a selected number of Days to an existing Date


mhoctober

Recommended Posts

Experts...

I have a start date in a database (fldDateCreated) and wish to add a number of days to it to calculate and store an end date in the database. The number of days to add to the start date is determined by the value that a user has selected from a form drop down field (frmAdDisplayFor)

//Code Snippet Start

$adDurationInDays=$_POST['frmAdDisplayFor'];
$expires=mysql_result($result,$i,"fldDateCreated")+$adDurationInDays;
$query = "INSERT INTO tblPeople(fldExpiryDate) VALUES ('$expires') where 'fldTransActionNumber' = $transNum";
mysql_query($query) or die('There is a problem: ' .mysql_error());

//Code Snippet End

This is the error message that I am getting when I run the code above...

"There is a problem: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where 'fldTransActionNumber' = 10016' at line 1"


I'm guessing that I have to do some further work with the variable $expires to convert it to a date before I can insert the value it holds into the table.  Any ideas?

Many thanks in advance....Mike


Link to comment
Share on other sites

Karma - thanks for the suggestion...which I have tried and still get the same error...

"There is a problem: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where `fldTransActionNumber` = 10018' at line 1"

Any further ideas?
Link to comment
Share on other sites

Thanks again....now I think I'm getting close to the solution!!

I am no longer getting an error message but the fldExpiryDate in the table is reading 0000-00-00

here is the revised code snippet....

$num=mysql_numrows($result);
$adDurationInDays=$_POST['frmAdDisplayFor']*7;
$expires=mysql_result($result,$i,"fldDateCreated")+$adDurationInDays;

echo "Weeks ".$_POST['frmAdDisplayFor']."<br>";
echo "Days ".$adDurationInDays."<br>";

$query = "Update tblPeople SET fldExpiryDate='$expires' where `fldTransActionNumber` = $transNum";

I'm still suspecting that I need to do some further work with the variable $expiry before I can store its values in the fldExpiryDate database field.

Thanks for the help
Link to comment
Share on other sites

you seem to be going about this in a very lengthy way.  allow MySQL to save you some effort:

[code]UPDATE tblPeople SET fldExpiryDate = DATE_ADD(fldExpiryDate, INTERVAL $adDurationInDays DAY) WHERE stuff[/code]

if you want to do it in weeks, it's just as easy.  change the interval type:

[code]UPDATE blah SET blah = DATE_ADD(blah, INTERVAL x WEEK) WHERE blah[/code]

anytime you're doing some database updates, before using PHP to get the value you want, see if MySQL has a built-in function for it.  their functions often save time and effort since it knows what format the value needs to be in, etc.

have a peek in the MySQL manual at "Functions and Operators" (chapter 7 i think).
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.