Jump to content

Recommended Posts

I am writing an application at the moment in PHP, and for the life of me i am struggling with this piece of logic.

 

Now, this particular function accepts a date in the form of a string as it comes out of MySQL (YYYY-MM-DD) (the date has already been checked for errors when inputted (i.e. no 31st day in feb, and leap year checking, etc) so assume the date passed is correct) but i must be having a bad day, as i cant mentally run through the statements (might have something to do with the hangover, but we will glaze over that)

 

OK Code is as follows (you will se how much of a programmers block i have when you see how simple the function is)

 

	$dob = split("-",$dob); // date arrived in YYYY-MM-DD
	$day = $dob[2]; // the day
	$month = $dob[1]; // the month
	$year = $dob[0]; // the year
	$current_year = date("Y"); // todays year (2010)
	$current_month = date("m"); // todays month (05)
	$current_day = date("d"); // todays day (26)
	$age = $current_year - $year; // deduct the birth year, from the current year to give us an age AFTER their birthday.
	if ($month >= $current_month) // this is where i get lost, the logic of finding their birthday.
	{
		if ($current_month == $month)
		{
			if ($day >= $current_day)
			{
				$age = $age - 1; // i dont think they have had a birthday yet, so we deduct 1 from the age.
			}
		}
		else
		{
			$age = $age - 1; // i dont think they have had a birthday yet, so we deduct 1 from the age.
		}
	}

 

Is my logic correct, or am i going mad?  :shrug:  :-\

Link to comment
https://forums.phpfreaks.com/topic/202990-is-my-logic-correct/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.