MySQL calculates the number of days, months, and years between two dates

MySQL calculates the number of days, months, and years between two dates

The MySQL built-in date function TIMESTAMPDIFF calculates the number of seconds, minutes, hours, days, weeks, quarters, months, years between two dates, and increases or decreases the current date by one day, a week, etc.

SELECT TIMESTAMPDIFF(type, start time, end time)

Difference in seconds:

SELECT TIMESTAMPDIFF(SECOND,'1993-03-23 ​​00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

Difference in minutes:

SELECT TIMESTAMPDIFF(MINUTE,'1993-03-23 ​​00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

Difference in hours:

SELECT TIMESTAMPDIFF(HOUR,'1993-03-23 ​​00:00:00 00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

Difference in days:

SELECT TIMESTAMPDIFF(DAY,'1993-03-23 ​​00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

Weeks difference:

SELECT TIMESTAMPDIFF(WEEK,'1993-03-23 ​​00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

Number of quarters difference:

SELECT TIMESTAMPDIFF(QUARTER,'1993-03-23 ​​00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

Difference in months:

SELECT TIMESTAMPDIFF(MONTH,'1993-03-23 ​​00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

Difference in years:

SELECT TIMESTAMPDIFF(YEAR,'1993-03-23 ​​00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

Get the current date:

SELECT NOW()
SELECT CURDATE()

Add one day to the current date:

SELECT DATE_SUB(CURDATE(),INTERVAL -1 DAY)

Subtract one day from the current date:

SELECT DATE_SUB(CURDATE(),INTERVAL 1 DAY)

Add one week to the current date:

SELECT DATE_SUB(CURDATE(),INTERVAL -1 WEEK)
SELECT DATE_SUB(NOW(),INTERVAL -1 MONTH)

Add one month to the current date:

SELECT DATE_SUB(CURDATE(),INTERVAL -1 MONTH)
FRAC_SECOND milliseconds SECOND seconds MINUTE minutes HOUR hours DAY days WEEK weeks MONTH months QUARTER quarters YEAR years

Summarize

The above is the introduction of MySQL to calculate the number of days, months, and years between two dates. I hope it will be helpful to everyone. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • How to query the maximum number of consecutive login days between two dates in MySQL
  • SQL query for users who have logged in for at least n consecutive days
  • mysql generates continuous dates and variable assignments
  • How to calculate the number of consecutive login days in MySQL

<<:  JS implements WeChat's "shit bombing" function

>>:  How to install JDK and set environment variables in Linux (this article is enough)

Recommend

Detailed analysis of Vue child components and parent components

Table of contents 1. Parent components and child ...

10 Best Practices for Building and Maintaining Large-Scale Vue.js Projects

Table of contents 1. Use slots to make components...

Nginx local directory mapping implementation code example

Sometimes you need to access some static resource...

Example of implementing login effect with vue ElementUI's from form

Table of contents 1. Build basic styles through E...

Introduction to generating Kubernetes certificates using OpenSSL

Kubernetes supports three types of authentication...

SQL interview question: Find the sum of time differences (ignore duplicates)

When I was interviewing for a BI position at a ce...

How to install Docker and configure Alibaba Cloud Image Accelerator

Docker Installation There is no need to talk abou...

Native JS to implement click number game

Native JS implements the click number game for yo...

How to open MySQL binlog log

binlog is a binary log file, which records all my...

Docker nginx example method to deploy multiple projects

Prerequisites 1. Docker has been installed on the...

Vue implements adding, displaying and deleting multiple images

This article shares the specific code for Vue to ...

How to create a MySQL database and support Chinese characters

Let's first look at the MySQL official docume...

Idea deploys remote Docker and configures the file

1. Modify the Linux server docker configuration f...

Complete steps to install MySQL 5.5 on CentOS

Table of contents 1. Preparation before installat...