In the following query the leap year is not taken into account.
SELECT e.id,
e.title,
e.birthdate
FROM employers e
WHERE DAYOFYEAR(curdate()) <= DAYOFYEAR(e.birthdate)
AND DAYOFYEAR(curdate()) +14 >= DAYOFYEAR(e.birthdate)
So in this query the birthdate of someone who is born in a leap year has got a different dayofyear in a non leap year.
How can i adjust the query to make sure it also works in a leap year?
The mysql version i have is: 5.0.67
There are 365 days in a normal year, 366 in a leap year. In a normal year, March 1 would be the 60th day of the year. In a leap year, February 29 would be the 60th day of the year. The MySQL function is consistent.
If you really wanted to make it more complicated than it has to be, you could add a day to your
DAYOFYEAR(curdate())
ifcurdate()
is greater than or equal to March 1 ANDcurdate()
isn't in a leap year. But I wouldn't recommend doing that.Here is a related solution
How to find the Birthday of FRIENDS Who are celebrating today using PHP and MYSQL celebrating-today-using-php-and-mysq
If I understand correctly, the problem you are having is that if your birthday is March 1, since you're looking for the (nth) 60th day of the year, you are getting the wrong day sometimes.
I believe the query in the above solution addresses the issue
Where
NOW()
is a non-leap year2011
, the problem arises from anybody born on a leap year after February 29 will have an extra day because you are usingDAYOFYEAR
against the birth year.Where you do
DAYOFYEAR
, you need the birthdate from the current year, not the year of birth.So, instead of:
You can convert it to this year like this:
Which converts a birthdate of:
To:
So, here's the modified query:
People born on February 29th will fall on March 1st on non-leap years, which is still day
60
.