Link to home
Start Free TrialLog in
Avatar of syedasimmeesaq
syedasimmeesaqFlag for United States of America

asked on

Date question figuring out a period

Hi
I am currently collecting dates as 1-1-2008, 10-1-2008 and such format
1-1-2008
m-d-yyyy

Now I am trying to figure this out

I need to make three periods of time.
Period1
period 1 will contain anything from month january to april
period2 will contain anything from month may till august
Period3 will contain anything from month september till december

so lets say a record has a date of 10-1-2008 it should fall under the timeperiod Period3
so my timeperiod firld should say Period3 in it.
How could I do that?
Thanks
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Split the date up and compare it like so

(UNTESTED)

$dt = explode( "-", $yourDateField);

switch( $dt[0] ) {

     case 1:
     case 2:
     case 3:
     case 4: $period = "Jan to April";
                 break;

     case 1:
     case 2:
     case 3:
     case 4: $period = "Jan to April";
                 break;

     case 5:
     case 6:
     case 7:
     case 8: $period = "May to Aug";
                 break;

     case 9:
     case 10:
     case 11:
     case 12: $period = "Sep to Dec";
                 break;

}
Oops - just noticed that part of the cases are repeated. Should be as below

$dt = explode( "-", $yourDateField);

switch( $dt[0] ) {

     case 1:
     case 2:
     case 3:
     case 4: $period = "Jan to April";
                 break;

     case 5:
     case 6:
     case 7:
     case 8: $period = "May to Aug";
                 break;

     case 9:
     case 10:
     case 11:
     case 12: $period = "Sep to Dec";
                 break;

}


An additional point is that you could do it

if ( $dt[0] >= 1 && $dt[0] <= 4 )
     $period = "period 1";
else
     if ( $dt[0] >= 5 && $dt[0] <= 8 )
         $period = "period 2";
     else
           if ( $dt[0] >= 9 && $dt[0] <= 12 )
                $period = "period 3";

but I feel that the use of the SWITCH gives clearer code that is easier to modify later.
     
Avatar of syedasimmeesaq

ASKER

sorry to reply so late but I got busy

This is what I think I want to do. Lets say today is Nov 19th, 2008. if the scrip can look for the current date and then echo out what period would it be

If its between Jan and April then its Springs
if its between May and August then its Fall
if its Sep and Dec then its Winters

any idea. Thanks
Well, since only only need the month we could mod it like so


$month = date("m");

switch( $month ) {

     case 1:
     case 2:
     case 3:
     case 4: $period = "Spring";
                 break;

     case 5:
     case 6:
     case 7:
     case 8: $period = "Fall";
                 break;

     case 9:
     case 10:
     case 11:
     case 12: $period = "Winter";
                 break;

}

echo "The period is $period";
well I found out I could do this

$my_t=getdate(date("U"));

print("$my_t[weekday], $my_t[month] $my_t[mday], $my_t[year]<br>");
$myYear = $my_t[year];
if ($my_t[mon] <= 2 ) { $season =  "Winter"; }// then you have spring
if ($my_t[mon] >= 3 and $my_t[mon] <= 6)  { $season =  "Spring"; }// then you have fall
if ($my_t[mon] >= 9 and $my_t[mon] <= 11){$season =  "Fall"; }
if ($my_t[mon]  = 12) { $season =  "Winter"; }
// then you have winter
echo $my_t[mon];

but only problem is that its November but when I echo $my_t[mon]; it shows me 12

whys that, do you know thanks
ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial