Question:
How do I add timestamp to a mysql recordset?
?
2011-07-26 15:42:09 UTC
My site is a forum for users to go in and post information. I'd like to add the date/time they posted next to their name but I'm unsure how to do it. It's written in php. (also I'm a new developer trying to teach myself everything so please be as simple and clear as possible :) thanks!!
Three answers:
rbjolly
2011-07-26 22:11:31 UTC
This type of thing is generally best handled in MySQL by setting a default value for your datetime field. Let's assume the table in question is called "yourTableName" and the field name is " posted_on". So to have MySQL handle this automatically when the record is inserted, you would modify your table structure by issuing a SQL command from a query browser, like so:



ALTER TABLE yourTableName MODIFY COLUMN posted_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP;



Now, if you wanted to have the timestamp field autmatically update when either an insert or update command was issues, you would use the following SQL command:



ALTER TABLE yourTableName MODIFY COLUMN posted_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;



So now MySQL will handle the creation of the timestamp, and you won't have to worry about managing it in your PHP code.
Taimoor Zaffar
2011-07-26 22:45:53 UTC
There is a function in PHP called time()





Use it like this :

"insert into posting(username,time) values($username,".time().")";



Here's the reference:

http://php.net/manual/en/function.time.php



Good luck!
2011-07-27 01:41:16 UTC
dont use time() if you want the current time. use the function now()


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...