Hello guys!

I hope you don't mind me posting so quick again. I am struggling or better don't seem to have proper idea about what to do here. I want to make a simple birthday reminder. I have got a list of people in my database and I log in using my id. All I want to do is to display a list of person and if they have got a birthday within the next seven days, display a notification.

I have written a simple function to do that but it doesn't work in the way I want and I can see the prob lies mainly with the month. Is there anything else I can do? Any ideas/help or even link to some tutorials will be gratefully appreciated.

Warm Regards
6pandn21

function CalculateAge($BirthDate)
      {
              list($Year, $Month, $Day) = explode("-", $BirthDate);
              $YearDiff = date("Y") - $Year;
              $MonthDiff= date("m") -$Month;
              $DayDiff = date("d") - $Day;
            if ($DayDiff < 8 && $MonthDiff == 0)
			{ $message = " ... this persons's birthday is within     the week! <br />";}
			else 
			{$message = "There is no birthday's within 7 days";}
			return $message;
      }

Recommended Answers

All 6 Replies

Hello guys!

I hope you don't mind me posting so quick again. I am struggling or better don't seem to have proper idea about what to do here. I want to make a simple birthday reminder. I have got a list of people in my database and I log in using my id. All I want to do is to display a list of person and if they have got a birthday within the next seven days, display a notification.

I have written a simple function to do that but it doesn't work in the way I want and I can see the prob lies mainly with the month. Is there anything else I can do? Any ideas/help or even link to some tutorials will be gratefully appreciated.

Warm Regards
6pandn21

function CalculateAge($BirthDate)
      {
              list($Year, $Month, $Day) = explode("-", $BirthDate);
              $YearDiff = date("Y") - $Year;
              $MonthDiff= date("m") -$Month;
              $DayDiff = date("d") - $Day;
            if ($DayDiff < 8 && $MonthDiff == 0)
			{ $message = " ... this persons's birthday is within     the week! <br />";}
			else 
			{$message = "There is no birthday's within 7 days";}
			return $message;
      }

Are you getting any errors? What do you mean does not work?

Are you sure all birthdays are stored in the database in the format
like YYYYY-MM-DD (1980-01-20)?

Also, you have to realize that if my birthday is on Jan 2 (for example, meaning in the first 6 days of the month), then you function will not work because you comparing current month difference like this: $MonthDiff = date("m") -$Month;
and in case a birthday is on 2nd of the month, and current date is 29th of the month, then the birthday is still within 7 days, but your function will fail because the months are different.

You are right, you better use strtotime($Burthday) - get the unix timestamp of the birthday first, then compare it to a timestamp of time() + 7 days
you have to calculate number of seconds in 7 days yourself.

Are you getting any errors? What do you mean does not work?

Are you sure all birthdays are stored in the database in the format
like YYYYY-MM-DD (1980-01-20)?

Also, you have to realize that if my birthday is on Jan 2 (for example, meaning in the first 6 days of the month), then you function will not work because you comparing current month difference like this: $MonthDiff = date("m") -$Month;
and in case a birthday is on 2nd of the month, and current date is 29th of the month, then the birthday is still within 7 days, but your function will fail because the months are different.

You are right, you better use strtotime($Burthday) - get the unix timestamp of the birthday first, then compare it to a timestamp of time() + 7 days
you have to calculate number of seconds in 7 days yourself.

Thank you very a resoundingly quick reply. This is just too fast:-) This forum just continues to impress me. Yes Uncle_smith, you are right about the month thing. I thought I will tell you but then again thought you guys will easily know there is a prob. I will try to do with the strtotime tom and I guess I will have more questions as I am a newbie in this php.

Warm Regards,
6pandn21

hello guys!

I changed my code but I guess there is something wrong as it shows there is birthday no matter which date. And when I change < to > it shows no birthdays. Waiting for your advice.

Warm Regards
6pandn21

function birthday($BirthDate)
      {
              list($Year, $Month, $Day) = explode("-", $BirthDate);
              $yeardiff = (date("Y")) - $Year;
              $monthdiff= (date("m")) -$Month;
              $daydiff = (date("d")) - $Day;
			if (monthdiff == 0) 
			{
				if (daydiff <= 0)
				{
				$year= date("Y");
				$bday=array($Year, $Month, $Day);
				$birthday=implode ("-", $bday);
				if ((strtotime($birthbday) - strtotime(now)) < 604800 )
				{echo "this persons bday is in the next 7 days";}
				}
				else
				{	
				$year=date("Y")+1;
				echo"$year";
				}} // added a 
				
			else if (monthdiff > 0)
			{ 
			$year = $date("Y")+1;
			echo"$year";
			}
				else {$year = date("Y");
				echo"$year";
				}
      }

No replies huh! Well anyways the tip was helpful and problem is basically solved. But I got a small problem which might be because of my lack of knowledge. My function returns a some value and there might be no value at all, now is it possible to do something like mysql_num_rows on it, so that I can echo some message saying there is no results for it? Or is there any other way to find if the function has returned empty result so that I can put some if condition. Thank you.
Warm Regards,
6pandn21

I think you answered your own question correctly. Use mysql_num_rows and if there 0 records found then you can take the appropriate action.

Chris

Thanks for the reply. solved it in a different way without checking condition of the function.

Warm Regards,
6pandn21

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.