flappy_warbucks Posted May 26, 2010 Share Posted May 26, 2010 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? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/202990-is-my-logic-correct/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 26, 2010 Share Posted May 26, 2010 $today = date("Y-m-d"); $age = substr($today, 0, 4) - substr($dob, 0, 4) - (substr($today, 5,5) < substr($dob, 5,5)); echo "DOB: $dob, Age: $age<br />"; You can also do this directly in your query using the same logic. Quote Link to comment https://forums.phpfreaks.com/topic/202990-is-my-logic-correct/#findComment-1063705 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.