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
LVL 9
syedasimmeesaqAsked:
Who is Participating?
 
Beverley PortlockConnect With a Mentor Commented:
The reason it shows 12 is because you are setting it to 12 in this

if ($my_t[mon]  = 12) { $season =  "Winter"; }

which should be

if ($my_t[mon]  == 12) { $season =  "Winter"; }
0
 
Beverley PortlockCommented:
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;

}
0
 
Beverley PortlockCommented:
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.
     
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
syedasimmeesaqAuthor Commented:
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
0
 
Beverley PortlockCommented:
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";
0
 
syedasimmeesaqAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.