Insert current date in datetime format mySQL
Solution 1:
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')");
Solution 2:
Try this instead
$date = date('Y-m-d H:i:s');
Solution 3:
you can use CURRENT_TIMESTAMP, mysql function
Solution 4:
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.
YYYY-MM-DD HH:mm:SS
Demonstration:
Following code shows the usage of NOW()
INSERT INTO auto_ins
(MySQL_Function, DateTime, Date, Time, Year, TimeStamp)
VALUES
(“NOW()”, NOW(), NOW(), NOW(), NOW(), NOW());
Solution 5:
set the type
of column named dateposted
as DATETIME
and run the following query:
INSERT INTO table (`dateposted`) VALUES (CURRENT_TIMESTAMP)