Detailed explanation of MySQL date addition and subtraction functions

Detailed explanation of MySQL date addition and subtraction functions

1. addtime()

Add the specified number of seconds to the date

select addtime(now(),1); -- add 1 second

2. adddate()

There are two ways to use it. If you fill in a 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); -- add 1 dayselect adddate(now(), interval 1 day); -- add 1 dayselect adddate(now(), interval 1 hour); -- add 1 hourselect adddate(now(), interval 1 minute); -- add 1 minuteselect adddate(now(), interval 1 second); -- add 1 secondselect adddate(now(), interval 1 microsecond); -- add 1 millisecondselect adddate(now(), interval 1 week); -- add 1 weekselect adddate(now(), interval 1 month); -- add 1 monthselect adddate(now(), interval 1 quarter); -- add 1 quarterselect adddate(now(), interval 1 year); -- add 1 year

3. date_add()

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

select date_add(now(), interval 1 day); -- add 1 dayselect date_add(now(), interval 1 hour); -- add 1 hourselect date_add(now(), interval 1 minute); -- add 1 minuteselect date_add(now(), interval 1 second); -- add 1 secondselect date_add(now(), interval 1 microsecond); -- add 1 millisecondselect date_add(now(), interval 1 week); -- add 1 weekselect date_add(now(), interval 1 month); -- add 1 monthselect date_add(now(), interval 1 quarter); -- add 1 quarterselect date_add(now(), interval 1 year); -- add 1 year

4. subtime()

Subtract the specified number of seconds from the date

select subtime(now(), 1); -- minus 1 second

5. subdate()

The usage is the same as adddate() function. There are two usages. If you fill in a number directly in the second parameter, it will subtract the specified number of days from the date. If you fill in interval, it will subtract the specified interval time from the date.

select subdate(now(),1); -- subtract 1 dayselect subdate(now(), interval 1 day); -- subtract 1 dayselect subdate(now(), interval 1 hour); -- subtract 1 hourselect subdate(now(), interval 1 minute); -- subtract 1 minuteselect subdate(now(), interval 1 second); -- subtract 1 secondselect subdate(now(), interval 1 microsecond); -- subtract 1 millisecondselect subdate(now(), interval 1 week); -- subtract 1 weekselect subdate(now(), interval 1 month); -- subtract 1 monthselect subdate(now(), interval 1 quarter); -- subtract 1 quarterselect subdate(now(), interval 1 year); -- subtract 1 year

6. date_sub()

Same as date_add() function, subtract a time interval from the date. This function can only use interval time as a parameter.

select date_sub(now(), interval 1 day); -- subtract 1 dayselect date_sub(now(), interval 1 hour); -- subtract 1 hourselect date_sub(now(), interval 1 minute); -- subtract 1 minuteselect date_sub(now(), interval 1 second); -- subtract 1 secondselect date_sub(now(), interval 1 microsecond); -- subtract 1 millisecondselect date_sub(now(), interval 1 week); -- subtract 1 weekselect date_sub(now(), interval 1 month); -- subtract 1 monthselect date_sub(now(), interval 1 quarter); -- subtract 1 quarterselect date_sub(now(), interval 1 year); -- subtract 1 year

This is the end of this article about the detailed explanation of MySQL date addition and subtraction functions. For more relevant MySQL date addition and subtraction function summary content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL date and time addition and subtraction sample code

<<:  Vue simulates the shopping cart settlement function

>>:  Linux performance monitoring tool nmon installation and usage tutorial analysis

Recommend

Solve the problem of ifconfig being unavailable in docker

Recently, when I was learning docker, I found tha...

5 ways to migrate from MySQL to ClickHouse

Data migration needs to be imported from MySQL to...

CSS code for arranging photos in Moments

First, you can open Moments and observe several l...

How to use MySQL covering index and table return

Two major categories of indexes Storage engine us...

Detailed explanation of MySQL DEFINER usage

Table of contents Preface: 1.Brief introduction t...

Linux concurrent execution is simple, just do it this way

Concurrency Functions time for i in `grep server ...

Detailed explanation of where the images pulled by docker are stored

The commands pulled by docker are stored in the /...

Teach you how to insert 1 million records into MySQL in 6 seconds

1. Idea It only took 6 seconds to insert 1,000,00...

CSS3 achieves conic-gradient effect

grammar: background-image: conic-gradient(from an...

How to create https using nginx and Tencent Cloud free certificate

I have been studying how to get https. Recently I...

36 principles of MySQL database development (summary)

Preface These principles are summarized from actu...

How to set up swap partition SWAP in Linux 7.7

The Swap partition of the Linux system, that is, ...

How to configure Nginx load balancing

Table of contents Nginx load balancing configurat...

Detailed explanation of jQuery's core functions and event handling

Table of contents event Page Loading Event Delega...