How can we know the number of days between two given dates using MySQL ? or find the difference between dates using MySQL ?
Do you ever faced this question in technical interview or technical test?
I know many of you have faced this....... :)
Here is a simple function which can be used to find the number of days between two given dates in MySQL.
Syntax - DATEDIFF(firstdate,seconddate)
so internally it will calculate the difference in MySQL by doing "firstdate minus seconddate", so if firstdate is greater than sencoddate then output will be positive and if firstdate is less than seconddate then output will be negative.
eg. if you use this function as
SELECT DATEDIFF('2009-11-19', '2009-10-15');
in MySQL
then you will get output as 35.
And if you pass parameters as
SELECT DATEDIFF('2009-10-15','2009-11-19')
then you will get output as -35.
If you know any other way to find the difference between dates then comments are always welcome.
Dont forget to tell your friends about http://programming-in-php.blogspot.com
MAKNYOSS
8 years ago