Advanced Search

Results 1 to 5 of 5

Thread: insert current date and time into mysql

  1. #1
    Join Date
    Jul 2007
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default insert current date and time into mysql

    hello all,

    i am working on making a blog.. this is just for me to learn php and mysql. so right now I am trying to make it post the time data to the database.
    here are the variables
    Code:
    $time= date("F j, Y, g:i a");
    i am using this code
    Code:
     
     mysql_query("INSERT INTO blog (message, username, date, time, title) VALUES('$blog', '$lusername', '$numdate', '$numtime', '$title'")
    or die("Error: ".mysql_error());
    so i was told to use
    Code:
    "SELECT NOW() As 'TheTime', someField FROM someTable" 
    
    $result->theTime
    $result->someField
    but i dont understand how to use that. so could someone explain what i am missing.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    Storing time in a database is almost always a case in which you would want to just store the timestamp using time().

    You can then use it to order the results when searching, etc., and format however you want later, rather than being stuck with a preformatted date.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jul 2007
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    Storing time in a database is almost always a case in which you would want to just store the timestamp using time().

    You can then use it to order the results when searching, etc., and format however you want later, rather than being stuck with a preformatted date.
    so how would i script that? thats my major issue...

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    I'm not really sure what that code is supposed to do either.

    For getting the time from the database, I'd use this:
    PHP Code:
    $query "SELECT 'time', 'name', 'other' FROM 'my_table_name';" //setup query
    $result mysql_query($query); //do mysql search
    while ($row mysql_fetch_array($result)) { //while there is a new row in the results
       //do something here, like echo, such as:
       
    echo $row['time']; //get time by name in the array
       
    echo $row[0]; //numerical way to get time, due to order in the query
       //do whatever....

    Check out http://php-mysql-tutorial.com to get more info on all of this.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Dec 2005
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by resnostyle View Post
    i am using this code
    Code:
     
     mysql_query("INSERT INTO blog (message, username, date, time, title) VALUES('$blog', '$lusername', '$numdate', '$numtime', '$title'")
    or die("Error: ".mysql_error());
    you are making it to complicated. use this.

    PHP Code:
    mysql_query("INSERT INTO blog (message, username, date, time, title) VALUES('$blog', '$lusername', NOW(), CURTIME(), '$title'")
    or die(
    "Error: ".mysql_error()); 
    that will get the current date and the current time. hope that helps

Bookmarks

Posting Permissions

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