In this tutorial we are writing MySQL query to sort upcoming birthdays based on current date, here writing three number of queries these may be help of to reach your result.
$sql = "SELECT * ,CASE WHEN BirthdayThisYear>=NOW() THEN BirthdayThisYear ELSE BirthdayThisYear + INTERVAL 1 YEAR END AS NextBirthday FROM ( SELECT * ,`emp_birthday` - INTERVAL YEAR(`emp_birthday`) YEAR + INTERVAL YEAR(NOW()) YEAR AS BirthdayThisYear FROM `hs_hr_employee` ) AS bdv WHERE `emp_birthday` != 'NULL' ORDER BY NextBirthday";
I tried to get the upcoming birthday from a employee table, for the next 90 days. Using :
$sql ="SELECT * FROM hs_hr_employee WHERE emp_status = '1' AND emp_birthday >= DATE_SUB(CURDATE(), INTERVAL YEAR(CURDATE()) - YEAR(emp_birthday) YEAR) AND emp_birthday <= DATE_ADD(DATE_SUB(CURDATE(), INTERVAL YEAR(CURDATE()) - YEAR(emp_birthday) YEAR), INTERVAL 365 DAY) ORDER BY emp_birthday";
$sql = "SELECT *, emp_birthday + INTERVAL(YEAR(CURRENT_TIMESTAMP) - YEAR(emp_birthday)) + 0 YEAR AS currbirthday, emp_birthday + INTERVAL(YEAR(CURRENT_TIMESTAMP) - YEAR(emp_birthday)) + 1 YEAR AS nextbirthday FROM hs_hr_employee WHERE emp_birthday !='NULL' AND emp_status='1' ORDER BY CASE WHEN currbirthday >= CURRENT_TIMESTAMP THEN currbirthday ELSE nextbirthday END";
My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.