Set a MySQL field with the current date (UNIX_TIMESTAMP(now))


For this, use unix_timestamp(). Let us first create a table −

mysql> create table DemoTable1894
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   DueTime int
   );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1894 values();
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1894 values();
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1894 values();
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1894;

This will produce the following output −

+----+---------+
| Id | DueTime |
+----+---------+
|  1 |    NULL |
|  2 |    NULL |
|  3 |    NULL |
+----+---------+
3 rows in set (0.00 sec)

Here is the query to set a field with UNIX Timestamp −

mysql> update DemoTable1894 set DueTime=unix_timestamp(now());
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable1894;

This will produce the following output −

+----+------------+
| Id | DueTime    |
+----+------------+
|  1 | 1576042722 |
|  2 | 1576042722 |
|  3 | 1576042722 |
+----+------------+
3 rows in set (0.00 sec)

Updated on: 27-Dec-2019

448 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements