The one before was inaccurate, displaying a one day and 3 hours difference on what should have been an hour.
This one should be accurate with any times
SELECT
@diff:=ABS( UNIX_TIMESTAMP("2014-05-09 21:24:25") - UNIX_TIMESTAMP() ) ,
CAST(@days := IF(@diff/86400 >= 1, floor(@diff / 86400 ),0) AS SIGNED) as days,
CAST(@hours := IF(@diff/3600 >= 1, floor((@diff:=@diff-@days*86400) / 3600),0) AS SIGNED) as hours,
CAST(@minutes := IF(@diff/60 >= 1, floor((@diff:=@diff-@hours*3600) / 60),0) AS SIGNED) as minutes,
CAST(@diff-@minutes*60 AS SIGNED) as seconds;
Explained:
- First we calculate the absolute difference in seconds and put that in a variable
- then we check if we get one or more of each (day hour minute), and if so we calculate how any we get and put that in a variable
- for units after the largest, we do the same, but first we subtract the seconds allotted to the previous unit from our overall difference
- Lastly we put whatever is left as the smallest unit, seconds