Greetings, folks...

Having an issue trying to figure something out logically and there's nothing I can easily find on the web... Here's an example:

Start Date: 5-15
End Date: 9-29

Question: Is 7-2 between the start and end date?

I get as far as narrowing down the month, but making sure is within the day range of the starting and ending months is what's killing me. Anyone have a function or logic that handles this?

    I suggest that you use [man]mktime[/man] or [man]date[/man] and check with a if-statement.

      Piranha wrote:

      I suggest that you use [man]mktime[/man] or [man]date[/man] and check with a if-statement.

      Here's the thing, and I'm not sure how this would work with mktime(), and that's that the year is going to be irrelevant -- perhaps if I provide more details:

      Here are three ranges:
      Range 1: 8.16 - 12.31
      Range 2: 1.1 - 5.20
      Range 3: 5.21 - 8.15

      So, I started off with something along these lines:

         
      $curMonth = date('m'); $curDay = date('j'); if($curMonth >= 8 && $curMonth <= 12) { } elseif($curMonth >= 1 && $curMonth <= 5) { } elseif($curMonth >= 5 && $curMonth <= 8) { }

      And it's within here, that I get stuck -- looking at it here, I can probably stick in more conditions (let's say with Range 3), if the month is 6-7, that's fine, but if it's 5, it has to be >= 21 and if it's 8 it has to be <= 15. I suppose I'm just trying to see if there's a more... elegant way to do this that I'm missing?

      Thanks,
      Rob

        Date comparison can be very simple...

        Let's use today's date : 2007-04-19 that we will write this way : 20071904

        Let's use my birthday this year : 2007-06-21, 20070621

        And now, I want to know if my departure to Vancouver (2007-05-10, 20070510) is between those two dates...

        $intDate1 = 20070419;
        $intDate2 = 20070621;
        $intDeparture = 20070510;
        if(($intDeparture >= min($intDate1, $intDate2)) && ($intDeparture <= max($intDate1, $intDate2))) {
            echo 'My departure is between today and my birthday !';
        } else {
            echo 'Looks like I might not celebrate my birthday in British Columbia :(';
        }
        

        The order must be : YearMonthDay, any of these can be optional of course as long as the other arguments remain in the same order...

          Use a dummy year. It is always the year 2000, then it would work except when it goes past the new year.

            Here's what I wound up with -- any idosyncracies you can see with this?

            function seasonTest()
            {
               $curMonth = date('m');
               $curDay   = date('d');
               $curYear  = date('Y');
            
               //We're going to use a dummy year, just for the calculation.
               $fallStart = 20000816;
               $fallEnd   = 20001231;
            
               $springStart = 20000101;
               $springEnd   = 20000520;
            
               $summerStart = 20000521;
               $summerEnd   = 20000815;
            
               $curDate = "2000$curMonth$curDay";
            
               if(($curDate >= min($fallStart, $fallEnd)) && ($curDate <= max($fallStart, $fallEnd)))
               {
                  echo "Fall";
               }
               elseif(($curDate >= min($springStart, $springEnd)) && ($curDate <= max($springStart, $springEnd)))
               {
                  echo "Spring";
               }
               elseif(($curDate >= min($summerStart, $summerEnd)) && ($curDate <= max($summerStart, $summerEnd)))
               {
                  echo "Summer";
               }
            }
            
            

              Well I don't know why fall starts on August 21st, spring on Jan 1st, summe on May 21st...

              But there will be a problem with winter since it changes year... then you would have to do some stuff like :

              if MonthDay >= 1201 and MonthDay <= 1231
              It's winter !
              else if MonthDay >= 0101 and MonthDay <= 0321
              It's winter
              ...

                suntra wrote:

                Well I don't know why fall starts on August 21st, spring on Jan 1st, summe on May 21st...
                ...

                These are actually college semesters, since, the Fall session will 'stop' at the end of the year and spring starts up on January 1st, this should alleviate any issues... Because I'm not really understanding what folks are saying about the year rolling over when, (I'm hoping) year shouldn't be at issue here.

                  I just wrote this for seasons...

                   $arrSeasons	= array(
                   					'0101'	=> 'Winter',
                   					'0321'	=> 'Spring',
                   					'0621'	=> 'Summer',
                   					'0921'	=> 'Fall',
                   					'1221'	=> 'Winter'
                   				);
                   $strToday		= date('md');
                   $strSeason		= '';
                   foreach($arrSeasons as $strDate => $strSeason1) {
                    	if($strToday >= $strDate) {
                    		$strSeason	= $strSeason1;
                  	} else {
                  		break;
                  	}
                   }
                   echo $strSeason;
                  

                  You can adapt it for your college use ;

                    Write a Reply...