How to insert current date/ time using now() in a field with MySQL?

MySQLMySQLi Database

In MySQL, now() can be used to insert current date/time. The syntax is as follows −

insert into yourTableName values(now());

To understand the above concept of inserting current date/time in the table, let us first create a table −

mysql> create table CurrentDateTimeDemo
   −> (
   −> YourTime datetime
   −> );
Query OK, 0 rows affected (0.58 sec)

Inserting current date/time using now(). The query is as follows −

mysql> insert into CurrentDateTimeDemo values(now());
Query OK, 1 row affected (0.20 sec)

Now you can check the current date/time has been inserted or not −

mysql> select *from CurrentDateTimeDemo;

The following output displays that the current date is successfully inserted using now():

+---------------------+
| YourTime            |
+---------------------+
| 2018-12-08 19:08:41 |
+---------------------+
1 row in set (0.00 sec)
raja
Published on 11-Jan-2019 14:33:34
Advertisements