Reputation: 2739
I have a facebook app that stores users birthday in mysql db as a varchar. Im trying to get all the users birthday that are
coming up in 1 week, if it's during the current week and if the birthday was last week.
in my php i get the birthday and convert them with strtotime()
I can easily check if the birthday is today
if (date('m-d') == date('m-d', $bday)) {
// today is your bday!!
}
Im lost when it comes to comparing dates aahaha I know I can use strtotime('nextweek') or something like that, but im not sure
Upvotes: 0
Views: 1923
Reputation: 47
If bday is based on the year of the person's birth, then I don't see how comparing to curdate() will ever give you an answer unless the person was less than 7 days old. =)
EDIT:
You should be able to do:
$bday_this_year = strtotime(date('Y') . '-' . date('m-d', $bday));
$bday_last_year = strtotime(date('Y')-1 . '-' . date('m-d', $bday));
$bday_next_year = strtotime(date('Y')+1 . '-' . date('m-d', $bday));
$last_week = strtotime("-1 week");
$next_week = strtotime("+1 week");
if (($bday_this_year > $last_week && $bday_this_year < $next_week) || ($bday_last_year > $last_week && $bday_last_year < $next_week) || ($bday_next_year > $last_week && $bday_next_year < $next_week)) {
// Happy Birthday duders!
}
The reason to compute the birthday across different years is to handle the edge cases where it's late December and the user's birthday is in January, or it's early January and the user's birthday is in late December, etc.
This answer could probably be refined, but it should work.
Upvotes: 1
Reputation: 15616
The query would be like :
select * from table
where bday between curdate() - INTERVAL 7 DAY and curdate() + INTERVAL 7 DAY
for the range within past week and next week.
Upvotes: 2