Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Senior Member medlington's Avatar
    Join Date
    Nov 2005
    Location
    Sheffield, UK
    Posts
    377
    Member #
    11968
    Liked
    4 times
    Hi,

    Im trying to write the current date to a Mysql DB when a user signs up, at the same time I need it to write the day 28 days in the future to the DB too.

    I can write the current date no problem with this code:

    PHP Code:
    $query_auto "INSERT INTO date (date)
                VALUE (CURDATE())"

    But I cant figure out how to write the code to add the date + 28 days

    Ive been searching and found the function:

    DATE_ADD(startDate, INTERVAL period periodType)

    but cant figure out how to use it properly.

    I have tried a few variations on this:


    PHP Code:
    $query_auto "INSERT INTO date (date)
        VALUE (DATE_ADD(CURDATE(), INTERVAL 28 DAYS) )"

    but cant get any of them to work.

    I think I could do it easily by writing the current date to the DB, then reading it back into a variable and adding 28 days to it with the function above. I guess I would substitute the 'startDate' with my variable and it would work? I would then write this new date to the DB. This seems like it would be ok but also seems abit long winded and Im sure I can put the CURDATE() in the function somehow but just dont understand the syntax.

    Any ideas?

  2.  

  3. #2
    Senior Member medlington's Avatar
    Join Date
    Nov 2005
    Location
    Sheffield, UK
    Posts
    377
    Member #
    11968
    Liked
    4 times
    And as if by magic, as soon as I post on here I find the answer, its amazing how many times that happens

    I just needed to remove the S from DAYS, so the code that works is:

    PHP Code:
    $query_auto "INSERT INTO date (date)
        VALUE (DATE_ADD(CURDATE(), INTERVAL 28 DAY) )"

    Hope that helps someone else

  4. #3
    Senior Member medlington's Avatar
    Join Date
    Nov 2005
    Location
    Sheffield, UK
    Posts
    377
    Member #
    11968
    Liked
    4 times
    Maybe I spoke too soon, when I try to use this code to update an existing field it just adds 0000-00-00 instead of the date.

    I think this is something to do with changing the INSERT to UPDATE

    this is the code Im using now:

    PHP Code:
    $query_date "UPDATE adverts SET 
                    date_approved='CURDATE()',
                    date_expire='DATE_ADD(CURDATE(), INTERVAL 28 DAY)',
                    approved='Yes'
                     WHERE id='
    $user_ID'";
                    
    mysql_query($query_date) or die(mysql_error()); 
    Now I am stuck, does anyone know why it should suddenly stop working?

  5. #4
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    CURDATE() is a function. You can't put it in quotes.

    DATE_ADD is flaky. Try just using +

    Code:
    $query_date = "UPDATE adverts SET 
                   date_approved = CURDATE(),
                   date_expire= CURDATE() + INTERVAL 28 DAY,
                   approved='Yes'
                   WHERE id='{$user_ID}'";

  6. #5
    Senior Member medlington's Avatar
    Join Date
    Nov 2005
    Location
    Sheffield, UK
    Posts
    377
    Member #
    11968
    Liked
    4 times
    Thanks that seems to work fine now


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:43 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com