I'm trying to count how many days and months are left for the next birthday given the date of birth by calculating it with the current date, but I can't get the days correctly, only the months, and I think I'm making it too 'long'.
How can I get the remaining days to the anniversary given the date?
Code
DROP FUNCTION IF EXISTS naixement;
DELIMITER //
CREATE FUNCTION naixement(par_data DATE)
RETURNS VARCHAR(25)
BEGIN
DECLARE var_messos INT DEFAULT FLOOR((1200 + DATE_FORMAT(NOW(),'%m%d') - DATE_FORMAT(par_data,'%m%d'))/100);
DECLARE var_dies INT DEFAULT FLOOR(TIMESTAMPDIFF( DAY, par_data, NOW()) % 30.4375);
RETURN CONCAT(var_messos,' mesos i ',var_dies,' dies');
END //
DELIMITER ;
Resolution
DECLARE var_mesos INT DEFAULT ABS(TIMESTAMPDIFF(MONTH, DATE(CONCAT(YEAR(NOW()),'/',MONTH(par_data),'/',DAY(par_data))),
DATE(CONCAT(YEAR(NOW()),'/',MONTH(NOW()),'/',DAY(par_data)))));
DECLARE var_dies INT DEFAULT ABS(TIMESTAMPDIFF(DAY, DATE(CONCAT(YEAR(NOW()),'/',MONTH(par_data),'/',DAY(par_data))),
DATE(CONCAT(YEAR(NOW()),'/',MONTH(par_data),'/',DAY(NOW())))));
In MySQL you can use the
DATEDIFF
.From the documentation (free translation from English...):