date_add(date_field, interval expr unit);
update dt_tb set dt2=date_add(dt,interval 3 month) WHERE id = 10
When we run this query the value of dt2 field for the record with id =10 will become 2007-04-22. This value has come from the value of dt field ( 2007-01-22) by adding 3 months. Same way let us try by adding 5 years to the dt field and updating the dt2 field
update dt_tb set dt2=date_add(now(),interval 5 year) where id = 10
Same way let us add 300 days
update dt_tb set dt2=date_add(dt,interval 300 day) where id = 10
We can add week and quarter to the date value by using date_add function. Here is the sample query.
update dt_tb set dt2=date_add(dt,interval 3 week) where id = 10
update dt_tb set dt2=date_add(dt,interval 3 quarter) where id = 10
We have used one update query as an example but this can be any other query type.
select date_add(now(),interval 15 day) as return_date
The above query will display date & time by adding 15 days to current date and time.
insert into dt_tb(dt,dt2) values(now(), date_add(now(),interval 15 day) )
The above query will add one record with issue date or current date and another field with return date by adding 15 days to current date.
SELECT dt, date_add( dt, INTERVAL 5 HOUR ) FROM dt_tb
Above query will display dt field values in one column and one more column by adding 5 hours to the time value of dt field. We can add various date formats to above query to display data based on our requirement. We will modify the above code to display only time part of the date data by using date_format function.
SELECT dt, date_format(date_add( dt, INTERVAL 5 HOUR ), '%T')FROM dt_tb
The above query can be modified to display other types of date formats.
CREATE TABLE `dt_tb` (
`id` int(2) NOT NULL auto_increment,
`dt` datetime NOT NULL default '0000-00-00 00:00:00',
`dt2` datetime NOT NULL default '0000-00-00 00:00:00',
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
--
-- Dumping data for table `dt_tb`
--
INSERT INTO `dt_tb` VALUES (1, '2007-02-15 00:00:00', '2005-01-25 00:00:00');
INSERT INTO `dt_tb` VALUES (2, '2007-02-12 23:56:54', '2005-06-12 00:00:00');
INSERT INTO `dt_tb` VALUES (10, '2007-01-22 00:00:00', '2007-04-22 00:00:00');
INSERT INTO `dt_tb` VALUES (11, '2009-03-15 07:56:14', '2009-03-30 07:56:14');
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.