tags:

views:

698

answers:

5

How can I add the current date and time (the date and time set on the server) to a MySQL table?

+1  A: 

CREATE TABLE t (ts TIMESTAMP ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP);

Scott Ferguson
A: 

Do you want to add it to the table or just to the result set? You can add NOW() to the field list of any query to do that.

If you just want to know what time the server thinks it is try:

SELECT NOW();

Edit: the original of this answer erroneously used "GETDATE()" 'cause I totally missed the MySQL tag. Matt Solnit called me on it, and rightly so.

MarkusQ
The GETDATE() function does not exist in MySQL.
Matt Solnit
My bad. I've gotten so used to all the MS* questions here I just missed the tag.
MarkusQ
That's what I figured ;-)
Matt Solnit
+2  A: 

Pretty sure there's a NOW() function... link

Mark
Yeah, this is the answer.. More on this @ http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_now
Tim
A: 

You could use PHP date, if that fits the scenario. Something like this:

$nowTime = date("Y-m-d H:i:s", time());
jsims281
A: 

use timestamp. easiest way, current_timestamp

terrific