Home >Database >Mysql Tutorial >Summary of MySQL date addition and subtraction functions

Summary of MySQL date addition and subtraction functions

藏色散人
藏色散人forward
2020-05-19 16:47:025953browse

Summary of MySQL date addition and subtraction functions

1. addtime() 

Add the specified number of seconds to the date

select addtime(now(),1); -- 加1秒

2. adddate( ) 

There are two ways to use it. If you fill in the number directly in the second parameter, it will add the specified number of days to the date. If you fill in interval, it will add the specified interval time to the date.

select adddate(now(),1); -- 加1天
select adddate(now(), interval 1 day); -- 加1天
select adddate(now(), interval 1 hour); --加1小时
select adddate(now(), interval 1 minute); -- 加1分钟
select adddate(now(), interval 1 second); -- 加1秒
select adddate(now(), interval 1 microsecond); -- 加1毫秒
select adddate(now(), interval 1 week); -- 加1周
select adddate(now(), interval 1 month); -- 加1月
select adddate(now(), interval 1 quarter); -- 加1季
select adddate(now(), interval 1 year); -- 加1年

3. date_add() 

Add a time interval to the date. This can only use interval time as a parameter. The usage is the same as adddate()

select date_add(now(), interval 1 day); -- 加1天
select date_add(now(), interval 1 hour); -- 加1小时
select date_add(now(), interval 1 minute); -- 加1分钟
select date_add(now(), interval 1 second); -- 加1秒
select date_add(now(), interval 1 microsecond); -- 加1毫秒
select date_add(now(), interval 1 week); -- 加1周
select date_add(now(), interval 1 month); -- 加1月
select date_add(now(), interval 1 quarter); -- 加1季
select date_add(now(), interval 1 year); -- 加1年

4 . subtime() 

Subtract the specified number of seconds from the date

select subtime(now(), 1); -- 减1秒

5. subdate() 

Usage with adddate() function Consistent, there are two ways to use it. If the second parameter is directly filled with numbers, the specified number of days is subtracted from the date. If the interval is filled in, the specified interval time is subtracted from the date.

select subdate(now(),1); -- 减1天
select subdate(now(), interval 1 day); -- 减1天
select subdate(now(), interval 1 hour); --减1小时
select subdate(now(), interval 1 minute); -- 减1分钟
select subdate(now(), interval 1 second); -- 减1秒
select subdate(now(), interval 1 microsecond); -- 减1毫秒
select subdate(now(), interval 1 week); -- 减1周
select subdate(now(), interval 1 month); -- 减1月
select subdate(now(), interval 1 quarter); -- 减1季
select subdate(now(), interval 1 year); -- 减1年

6. date_sub()  

The usage is consistent with the date_add() function, subtracting a time interval from the date. This can only use interval time as a parameter

select date_sub(now(), interval 1 day); -- 减1天
select date_sub(now(), interval 1 hour); --减1小时
select date_sub(now(), interval 1 minute); -- 减1分钟
select date_sub(now(), interval 1 second); -- 减1秒
select date_sub(now(), interval 1 microsecond); -- 减1毫秒
select date_sub(now(), interval 1 week); -- 减1周
select date_sub(now(), interval 1 month); -- 减1月
select date_sub(now(), interval 1 quarter); -- 减1季
select date_sub(now(), interval 1 year); -- 减1年

Recommended: "mysql tutorial

The above is the detailed content of Summary of MySQL date addition and subtraction functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete