Insert current date in datetime format mySQL

04/20/2020 12:30:01

I'm having problems getting the date inserted properly into my database.

$date = date('m/d/Y h:i:s', time());

I use this format, and, it echoes out correctly, however, when, I insert

mysql_query("INSERT INTO table 
VALUES ('$date')");

it doesn't appear to work successfully, and, the time remains 00:00:00 If you could find the solution that would be great, thanks.

Verified Answer (213 Votes)

03/03/2012 03:11:25

If you're looking to store the current time just use MYSQL's functions.

mysql_query("INSERT INTO `table` (`dateposted`) VALUES (now())");

If you need to use PHP to do it, the format it Y-m-d H:i:s so try

$date = date('Y-m-d H:i:s');
mysql_query("INSERT INTO `table` (`dateposted`) VALUES ('$date')");

Answer #2 (36 Votes)

03/03/2012 03:11:16

Try this instead

$date = date('Y-m-d H:i:s');

Answer #3 (6 Votes)

10/19/2012 11:02:52

NOW() is used to insert the current date and time in the MySQL table. All fields with datatypes DATETIME, DATE, TIME & TIMESTAMP work good with this function.



Following code shows the usage of NOW()

INSERT INTO auto_ins
(MySQL_Function, DateTime, Date, Time, Year, TimeStamp)
(“NOW()”, NOW(), NOW(), NOW(), NOW(), NOW());
Hack Hex uses Stack Exchance API by the Stack Exchange Inc. to scrape questions/answers under Creative Commons license.