Calculate days between two dates in MySQL with example

MySQL DATEDIFF() function returns the number of days between the two dates. The DATEDIFF() function takes two arguments date1 and date2.

  • DATEDIFF(Date1, Date2)

Example : 1 – In this example, the days are calculated from current date.

SELECT DATEDIFF(CURDATE(), '2020-02-14');

Example : 2 – In this example, the days are calculated between two dates.

SELECT DATEDIFF('2020-04-30', '2020-01-01'); // date format Y-m-d

Example : 3 – In this example, the days are calculated between two dates & time.

SELECT DATEDIFF('2020-04-30 10:11:20', '2020-01-01 11:45:22'); // date format Y-m-d h:m:s

Example : 4 – In this example, the total days are calculated between two columns names when column type is ‘datetime‘ in the MySQL table.

student_experience Table
SELECT DATEDIFF(exp_to_date, exp_from_date) + 1 AS TotalDays FROM student_experience;  // include one day
//Output is 30 days

Example : 5 -In this example, the total days are calculated between two columns names when column type is ‘date‘ in the MySQL table.

student_experience Table
SELECT DATEDIFF(exp_to_date, exp_from_date) + 1 AS TotalDays FROM student_experience;  // include one day
//Output is 30 days

Love Quotes in Hindi … Click here …

Calculate days between two dates in MySQL with example

Leave a Reply

Your email address will not be published. Required fields are marked *