www.webdeveloper.com
Results 1 to 5 of 5

Thread: adding 24 hours to current time and saving it to mysql database

  1. #1
    Join Date
    Aug 2009
    Location
    Ireland
    Posts
    25

    adding 24 hours to current time and saving it to mysql database

    Hi,

    Sorry i'm very new to php and I think this might be a very basic question.

    I'm trying to add 24 hours to the current time and save it in a mysql database.

    Code:
    <?
    $time1 = time () + 24 * 60 * 60;
    ?>
    At the moment im just gettin a series of 0s when I save to the database

    The column im trying to save it in, is of datatype "timestamp".

    So I think i need to convert the value stored in $time1 to a timestamp format?

    Can anyone suggest if I'm going about this in the right way? and if I am how do I go about making the conversion.

    If anyone has any suggestions it would be a massive help.

    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,636
    PHP Code:
    $timePlus24Hr date('Y-m-d H:i:s'time() + 24 60 60); 
    Or you could let MySQL do the work:
    Code:
    INSERT INTO `table` (`col_name`) VALUES ( DATE_ADD( NOW(), INTERVAL 24 HOUR ) )
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2009
    Location
    Ireland
    Posts
    25
    Thanks for getting back to me NogDog.

    This works perfectly.

    Cheers!

  4. #4
    Join Date
    Apr 2008
    Posts
    157
    This is so much easier:

    Code:
    strtotime("+1 days");
    strtotime("tomorrow");
    strtotime("+5 hours");

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,636
    Quote Originally Posted by Teufel View Post
    This is so much easier:

    Code:
    strtotime("+1 days");
    strtotime("tomorrow");
    strtotime("+5 hours");
    It's a very useful function, but you still need to convert it to a "yyyy-mm-dd hh:mm:ss" string to be used in the SQL.
    PHP Code:
    $sqlTime date('Y-m-d H:i:s'strtotime('+24 hours')); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles