Solved

# Date question figuring out a period

Posted on 2008-11-07
179 Views
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
0
Question by:syedasimmeesaq

LVL 34

Expert Comment

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

LVL 34

Expert Comment

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

LVL 9

Author Comment

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

LVL 34

Expert Comment

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

LVL 9

Author Comment

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

LVL 34

Accepted Solution

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

## Featured Post

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…