I racked my brain over this problem and though I came up with a solution I'm sure I made it way too complicated.
I have a database with a table of Seasons which includes the season name, the month and day it starts and the month and day it ends. I have tied another table of data with dates and I want to bring back those dates by season. Unfortunately a season can span the year end. Winter for example would be set up in the database with the dates 11/15-3/31. This is for a site that lists travel packages in case anyone is curious. The table with the dates in it has actual dates with the year included.
My problem was trying to first determine which season the site viewer was currently in according to the current date. Then I wanted to sort the seasons in a logical order but starting with the current season. So, for example, if you looked at the site in the Summer of '03, the order of the seasons would be Summer '03, Fall '03, Winter '03-'04, Sprint '04.
I just wanted it to be such that you are always looking forward from the current season. I used the season order in a menu.
After sorting the seasons I needed to go back and assign a year to the season beginning and ending dates so I had actual dates I could use in my SQL queries to bring back the data from my other tables that fell within the season dates.
Anyway what I came up with has a lot of if/then statements and I am sure there is a much more efficient way to do this. Here is the code. Any suggestions would be appreciated. Thanks.
<?PHP
if(!$DBH)
{
include(getenv("DOCUMENT_ROOT").'/common/include/opendb_inc.php');
}
$thisYear=date('Y');
$nextYear=$thisYear+1;
$thisMonth=date('n');
$thisDay=date('j');
$sorted='false';
//Get Seasons
$sqlQuery="SELECT ID, Title, StartMonth, StartDay, EndMonth, EndDay FROM tPackageSeasons WHERE Active='Y' ORDER BY StartMonth";
$result=$query($sqlQuery);
if (mysql_errno())
{
$result=db_error($errnofunc(),$errorfunc(),'','','');
$message.=$result;
if($DEBUG){trigger_error($result);}
}
else
{
while($row=$fetch_array($result))
{
$seasons[$row['ID']]=array(
'ID'=>$row['ID'],
'Title'=>$row['Title'],
'StartMonth'=>$row['StartMonth'],
'StartDay'=>$row['StartDay'],
'EndMonth'=>$row['EndMonth'],
'EndDay'=>$row['EndDay']
);
$seasonOrder[]=$row['ID'];
}
}
//Sort Seasons
do{
if($seasons[$seasonOrder[0]]['StartMonth'] > $seasons[$seasonOrder[0]]['EndMonth'])
{
if($seasons[$seasonOrder[0]]['StartMonth'] < $thisMonth)
{
$sorted='true';
}
elseif($seasons[$seasonOrder[0]]['StartMonth'] == $thisMonth)
{
if($seasons[$seasonOrder[0]]['StartDay'] <= $thisDay)
{
$sorted='true';
}
}
elseif($seasons[$seasonOrder[0]]['EndMonth'] > $thisMonth)
{
$sorted='true';
}
elseif($seasons[$seasonOrder[0]]['EndMonth'] == $thisMonth)
{
if($seasons[$seasonOrder[0]]['EndDay'] >= $thisDay)
{
$sorted='true';
}
}
}
else
{
if($seasons[$seasonOrder[0]]['StartMonth'] < $thisMonth)
{
if($seasons[$seasonOrder[0]]['EndMonth'] > $thisMonth)
{
$sorted='true';
}
elseif($seasons[$seasonOrder[0]]['EndMonth'] == $thisMonth)
{
if($seasons[$seasonOrder[0]]['EndDay'] >= $thisDay)
{
$sorted='true';
}
}
}
elseif($seasons[$seasonOrder[0]]['StartMonth'] == $thisMonth)
{
if($seasons[$seasonOrder[0]]['StartDay'] <= $thisDay)
{
if($seasons[$seasonOrder[0]]['EndMonth'] > $thisMonth)
{
$sorted='true';
}
elseif($seasons[$seasonOrder[0]]['EndMonth'] == $thisMonth)
{
if($seasons[$seasonOrder[0]]['EndDay'] >= $thisDay)
{
$sorted='true';
}
}
}
}
}
if($sorted=='false')
{
//Push to End of Array
$temp=array_shift($seasonOrder);
array_push($seasonOrder,$temp);
}
}WHILE($sorted=='false');
}
//Set Years for Each Season
for($i=0;$i<count($seasonOrder);$i++)
{
if($seasons[$seasonOrder[$i]]['EndMonth'] < $thisMonth)
{
if($seasons[$seasonOrder[$i]]['EndMonth'] < $seasons[$seasonOrder[$i]]['StartMonth'])
{
$seasons[$seasonOrder[$i]]['Year']=$thisYear;
}
else
{
$seasons[$seasonOrder[$i]]['Year']=$nextYear;
}
}
elseif($seasons[$seasonOrder[$i]]['EndMonth'] == $thisMonth)
{
if($seasons[$seasonOrder[$i]]['EndDay'] < $thisDay)
{
$seasons[$seasonOrder[$i]]['Year']=$nextYear;
}
else
{
$seasons[$seasonOrder[$i]]['Year']=$thisYear;
}
}
else
{
$seasons[$seasonOrder[$i]]['Year']=$thisYear;
}
}
?>