431,919 Members | 1,587 Online
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

# Detect Seasons with a given date interval

 100+ P: 638 i have the following code through which i need to detect a season . [code] Expand|Select|Wrap|Line Numbers function season(\$period){ \$seasons    = array( 'spring'    => array('March 21'     , 'June 20'), 'summer'    => array('June 21'      , 'September 22'), 'fall'      => array('September 23' , 'December 20'), 'winter'    => array('December 21'  , 'March 20') );           \$seasonsYear = array();           \$start      = strtotime(\$period[0]);         \$end        = strtotime(\$period[1]);           \$seasonsYear[date('Y', \$start)] = array();             if (key(current(\$seasonsYear)) != date('Y', \$end))             \$seasonsYear[date('Y', \$end)] = array();           foreach (\$seasonsYear as \$year => &\$seasonYear){             foreach (\$seasons as \$season => \$period){                 \$seasonYear[\$season] = array(strtotime(\$period[0].' '.\$year), strtotime(\$period[1].' '.(\$season != 'winter' && \$season != 'Winter2' ? \$year : (\$year+1))));               }         }           foreach (\$seasonsYear as \$year => &\$seasons) {             foreach (\$seasons as \$season => &\$period) {                 if (\$start >= \$period[0] && \$end <= \$period[1]){                     return ucFirst(\$season);                 }                   if (\$start >= \$period[0] && \$start <= \$period[1]) {                     if (date('Y', \$end) != \$year)                          \$seasons = \$seasonsYear[date('Y', \$end)];                            \$year = date('Y', \$end);                       \$nextSeason = key(\$seasons);                     \$nextPeriod = current(\$seasons);                                     do {                                             \$findNext   = (\$end >= \$nextPeriod[0] && \$end <= \$nextPeriod[1]);                           \$nextSeason = key(\$seasons);                         \$nextPeriod = current(\$seasons);                     } while (\$findNext = False);                       \$diffCurr   = \$period[1]-\$start;                     \$diffNext   = \$end-\$nextPeriod[0];                       if (\$diffCurr > \$diffNext)                         return ucFirst(\$season);                     else {                         return ucFirst(\$nextSeason);                     }                 }             }         }     } echo season(array('2011-12-24','2011-12-26'));   now the problem is that the client does not want to have the standard season interval dates he wants Winter to have interval from Nov 15th to May 15 Christmas to have interval from Dec 24 - Jan 1 and rest of the days to Summer now when i try to replace the default \$seasons array with the following every thing messes up Expand|Select|Wrap|Line Numbers 'summer'    => array('May 16'      , 'November 16'), 'winter'    => array('November 15'  , 'December 23'), 'christmas' => array('December 24' , 'January 1'), 'winter'    => array('January 2'  , 'May 15')   i am really stuck why is this happening , at first it was tat i was using foreach loop for seasons and it only looped though the 3 indexes and even showed the sizeof \$seasonYear equal to 3, until i changed the name of the last index Winter to Winter2 , but didnt not detect the right season even then may be it is because of the christmas season which falls between winter and winter2 any help in regarding this problem would be appreciated regards, Omer Aslam Nov 1 '11 #1