View Full Version : insert current date and time into mysql
resnostyle
07-18-2007, 05:07 AM
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
$time= date("F j, Y, g:i a");
i am using this 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
"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.
djr33
07-18-2007, 05:11 AM
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.
resnostyle
07-18-2007, 05:44 AM
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...
djr33
07-18-2007, 05:57 AM
I'm not really sure what that code is supposed to do either.
For getting the time from the database, I'd use this:
$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.
jstgermain
07-29-2007, 10:33 PM
i am using this 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.
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
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.