To insert the current date (not the time), you can use MySQL's built-in function CURDATE(). The syntax is as follows -
INSERT INTO yourTableName values(curdate());
Alternatively, if you want to add date and time, then you can use MySQL's built-in function NOW(). The syntax is as follows -
INSERT INTO yourTableName values(now());
To understand these two syntaxes, we first create a table. The query to create the table is as follows -
mysql> create table NowAndCurdateDemo −> ( −> YourDueDate datetime −> ); Query OK, 0 rows affected (1.75 sec)
Implement the function to insert the current date and date time into the table. The query to insert date is as follows -
mysql> insert into NowAndCurdateDemo values(curdate()); Query OK, 1 row affected (0.28 sec) mysql> insert into NowAndCurdateDemo values(now()); Query OK, 1 row affected (0.14 sec)
Use the following query to check if the data is inserted into the table -
mysql> select *from NowAndCurdateDemo;
The following is the output -
+---------------------+ | YourDueDate | +---------------------+ | 2018-12-05 00:00:00 | | 2018-12-05 21:24:10 | +---------------------+ 2 rows in set (0.06 sec)
The above is the detailed content of Insert current date in datetime format MySQL?. For more information, please follow other related articles on the PHP Chinese website!