Is there any method in mysql database which return the difference between two dates in months and days. for eg if i provide 01-01-2018 as fromdate and 02-02-2018 as todate i want to get the result as 1 month 1 day.
            Asked
            
        
        
            Active
            
        
            Viewed 40 times
        
    0
            
            
        - 
                    https://stackoverflow.com/questions/288984/the-difference-in-months-between-dates-in-mysql – Tum Jan 29 '18 at 10:30
- 
                    i want month as well as day. Not month alone. the link u provided talks only about month. i want to have both in one result. – user3292629 Jan 29 '18 at 10:33
- 
                    then put `DAY` or `DAYOFMONTH` instead of `MONTH`; and if you want both, just put one with `MONTH` and put another one with `DAYOFMONTH` :) – NatNgs Jan 29 '18 at 10:38
- 
                    i want the difference between two dates in month and day. if i give 01-01-2018 as fromDate and 02-02-2018 as toDate i want to get the output as 1 month 1 day. Is there any method like that. – user3292629 Jan 29 '18 at 10:42
1 Answers
0
            
            
        SELECT FLOOR(DATEDIFF('2018-02-01', '2018-01-01')/30) AS months, DATEDIFF('2018-02-01', '2018-01-01')%30 AS days;
The only problem is that is calculates months as 30 days. So it doesn't take into account how many days the current month is.
UPDATE:
A solution for taking month length into account
SELECT 
(CASE WHEN (DAY('2018-02-02') >= DAY('2018-01-01')) 
THEN
    (YEAR('2018-02-02')*12 + MONTH('2018-02-02')) - (YEAR('2018-01-01')*12 + MONTH('2018-01-01'))
ELSE
    (YEAR('2018-02-02')*12 + MONTH('2018-02-02')) - (YEAR('2018-01-01')*12 + MONTH('2018-01-01')) - 1
END) AS months,
(CASE WHEN (DAY('2018-02-02') >= DAY('2018-01-01')) 
THEN
    DAY('2018-02-02') - DAY('2018-01-01')
ELSE
    (DAY(LAST_DAY('2018-01-01')) - DAY('2018-01-01')) + DAY('2018-02-02')
END) AS days;
You should insert the dates as a variable (replace '2018-01-01' with fromdate and '2018-02-02' with todate).
Explanation:
It has 2 cases for both months and days:
- Case 1: the day of the month of todateis higher than (or equal to) the date of the month offromdate. In this case the number of months is the number of years * 12 + the number of months oftodate, minus the same calculation forfromdate. The number of days is simply the day number oftodateminus the day number offromdate.
- Case 2: the day of the month of todateis lower than the date of the month offromdate. In this case the number of months is the number of years * 12 + the number of months oftodate, minus the same calculation forfromdate, minus 1 (because the month isn't 'full'). The number of days is the last day of the month offromdate, minus the day offromdate, plus the day of the month oftodate. Or in other words: the amount of days passed in the month offromdatesince the day of that month, plus the days passed in the month oftodate.
 
    
    
        Tum
        
- 6,937
- 2
- 25
- 23
- 
                    
- 
                    Updated my answer, felt like solving this puzzle :-) let me know if it does everything you need! – Tum Jan 30 '18 at 11:21
