Jump to content

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

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.