Jump to content

Date conditions


acmumph

Recommended Posts

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;
           

Link to comment
https://forums.phpfreaks.com/topic/243659-date-conditions/
Share on other sites

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 :D

Link to comment
https://forums.phpfreaks.com/topic/243659-date-conditions/#findComment-1251067
Share on other sites

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..

Link to comment
https://forums.phpfreaks.com/topic/243659-date-conditions/#findComment-1251079
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.