MySQL DateTime Now()+5 days/hours/minutes/seconds?


To update the current date and time to 5 days, you need to use the Now() + 5. That would update the entire date-time i.e. days, hour, minutes and seconds. To understand this, let us create a table. The query to create a table is as follows −

mysql> create table UserInformationExpire
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> UserName varchar(10),
   -> UserInformationExpireDateTime datetime not null
   -> );
Query OK, 0 rows affected (0.83 sec)

Now you can insert some records in the table using insert command. The query is as follows −

mysql> insert into UserInformationExpire(UserName,UserInformationExpireDateTime)
values('Maxwell','2019-02-09 23:56:27');
Query OK, 1 row affected (0.22 sec)
mysql> insert into UserInformationExpire(UserName,UserInformationExpireDateTime)
values('Carol','2018-11-21 15:45:21');
Query OK, 1 row affected (0.24 sec)
mysql> insert into UserInformationExpire(UserName,UserInformationExpireDateTime)
values('Bob','2019-01-22 16:30:35');
Query OK, 1 row affected (0.25 sec)
mysql> insert into UserInformationExpire(UserName,UserInformationExpireDateTime)
values('Larry','2017-12-25 17:20:33');
Query OK, 1 row affected (0.20 sec)

Display all records from the table using a select statement. The query is as follows −

mysql> select *from UserInformationExpire;

Output

+----+----------+-------------------------------+
| Id | UserName | UserInformationExpireDateTime |
+----+----------+-------------------------------+
|  1 | Maxwell  | 2019-02-09 23:56:27           |
|  2 | Carol    | 2018-11-21 15:45:21           |
|  3 | Bob      | 2019-01-22 16:30:35           |
|  4 | Larry    | 2017-12-25 17:20:33           |
+----+----------+-------------------------------+
4 rows in set (0.00 sec)

Here is your syntax to update the current datetime to 5 days/hours/minutes/seconds for id = 1 i.e. the current date is 2019-02-09 and it will be updated to 2019-02-14

mysql> update UserInformationExpire set UserInformationExpireDateTime = now()+interval 5
day where Id = 1;
Query OK, 1 row affected (0.24 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Now check the table records once again to verify the date and time has been updated for only id 1 −

mysql> select *from UserInformationExpire where Id = 1;

Output

+----+----------+-------------------------------+
| Id | UserName | UserInformationExpireDateTime |
+----+----------+-------------------------------+
|  1 | Maxwell  | 2019-02-14 23:56:27           |
+----+----------+-------------------------------+
1 row in set (0.00 sec)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

405 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements