acmumph Posted August 3, 2011 Share Posted August 3, 2011 So I'm trying to assign classes based on date of birth(dob)..For example if born within last 4 months you are in class 1, if born between 8-12 months ago class 2 and 12-16 months ago class 3....Appreciate your time... <?php $dob=$row['dob']; $format='j-M-y'; $date=date($format); $classdate1= date ($format, strtotime('-4 month' .$date)); $classdate2= date ($format, strtotime('-8 month' .$date)); $classdate3= date ($format, strtotime('-12 month' .$date)); $classdate4= date ($format, strtotime('-16 month' .$date)); $classdate5= date ($format, strtotime('-20 month' .$date)); $classdate6= date ($format, strtotime('-24 month' .$date)); $classdate7= date ($format, strtotime('-28 month' .$date)); if ($dob < $classdate1) { $class = 1; echo $class; } elseif ( $dob > $classdate1 && $dob < $classdate2) { $class = 2; echo $class; } elseif ( $dob > $classdate2 && $dob < $classdate3) { $class = 3; echo $class; Quote Link to comment https://forums.phpfreaks.com/topic/243659-date-conditions/ Share on other sites More sharing options...
phpSensei Posted August 3, 2011 Share Posted August 3, 2011 $months = floor((($end_date - $start_date) % 31556926) Given the end and start date will return the number of months between two dates. So in your case take the current date with the user's dob. Quote Link to comment https://forums.phpfreaks.com/topic/243659-date-conditions/#findComment-1251054 Share on other sites More sharing options...
xyph Posted August 3, 2011 Share Posted August 3, 2011 Do you mean a month in the classical sense? Or can a month be generically 30 days? Months are hard because the actual amount of time between, say, June 12 and July 12 is different than February 12 and March 12. You're better off saying between 1-120 days, 121-240, etc. Makes the math so much easier Quote Link to comment https://forums.phpfreaks.com/topic/243659-date-conditions/#findComment-1251067 Share on other sites More sharing options...
acmumph Posted August 3, 2011 Author Share Posted August 3, 2011 Seems the 1-120..121-240 would be the way to go...For some reason I can't get the logic to work out in my code attempt, but it sounds good when I say it out loud .. Quote Link to comment https://forums.phpfreaks.com/topic/243659-date-conditions/#findComment-1251071 Share on other sites More sharing options...
acmumph Posted August 3, 2011 Author Share Posted August 3, 2011 I found a solution. I handled it in mysql using SELECT DATEDIFF(CURDATE(), dob) as days FROM table From there I just added my conditions.. $days=$row['days']; if ($days <=120) { $class =1; echo $class; } elseif ($days >120 && $days <=240) { $class=2; echo $class; } elseif ($days >240 && $days <=360) { $class=3; echo $class; } Appreciate the help.. Quote Link to comment https://forums.phpfreaks.com/topic/243659-date-conditions/#findComment-1251079 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.