Home > Database > Mysql Tutorial > How to Calculate the Difference in Days Between Two Dates in MySQL?

How to Calculate the Difference in Days Between Two Dates in MySQL?

Susan Sarandon
Release: 2024-12-30 18:04:10
Original
925 people have browsed it

How to Calculate the Difference in Days Between Two Dates in MySQL?

Calculating Day Difference Between Two Dates in MySQL

Question:

How can one determine the number of days between two given dates using MySQL?

Answer:

The DATEDIFF function is an effective tool for calculating the day difference between two dates. According to the MySQL documentation, DATEDIFF "returns expr1 – expr2 expressed as a value in days from one date to the other."

Implementation:

To find the day difference, use the following syntax:

select datediff(end_date, start_date)
Copy after login

Where:

  • end_date and start_date represent the dates for which you want to calculate the difference.

Example:

Let's consider the example provided in the question:

  • Check-in date: '2010-04-12'
  • Check-out date: '2010-04-15'

Using DATEDIFF, we can calculate the day difference as follows:

select datediff('2010-04-15', '2010-04-12');
Copy after login

This query will return the result 3, indicating the 3-day difference between the two dates.

Note:

Keep in mind that dates must be formatted as YYYY-MM-DD in MySQL.

The above is the detailed content of How to Calculate the Difference in Days Between Two Dates in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template