Need help with auto inserting date and time

Discussion in 'PHP' started by alterman, Dec 8, 2008.

  1. #1
    I'am building comments managment system and i need to store in mysql database current comment time and date on submit.
    Please help me to solve this.
     
    alterman, Dec 8, 2008 IP
  2. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    get the date from php server and use that in an INSERT/UPDATE command; call date ('r', time()) and insert as field value.

    OR, let the sql server insert the current date time: ", NOW()," as field value
     
    rene7705, Dec 8, 2008 IP
  3. Nightcoder

    Nightcoder Active Member

    Messages:
    521
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Yeah rene7705 is right, all you have to do is something like below...
    
    <?
    $date = date("F j, Y, g:i a");   
    $whatever =mysql_query("INSERT INTO `table_name` (`id` , `date`) VALUES ('', '$date')");  
    ====================================
    Or to update a table just do this..
    ====================================
    $date = date("F j, Y, g:i a");   
    $whatever =mysql_query("UPDATE `table_name` SET `date` = '".$date." WHERE `id` ='$id'");
    
    ?>
    
    
    PHP:
    This would insert the date like this: March 10, 2001, 5:16 pm

    Hope it helps...
     
    Nightcoder, Dec 11, 2008 IP
  4. GLD

    GLD Well-Known Member

    Messages:
    307
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    140
    #4
    It would be more practical to save the date as a timestamp. This would make it easier to output the date in various formats.
     
    GLD, Dec 11, 2008 IP
  5. shadow_boi

    shadow_boi Peon

    Messages:
    374
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi,

    I am not quite sure how to use NOW().

    So just need to enter NOW() as the default value filed in table in mysql?
     
    shadow_boi, Dec 11, 2008 IP