By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,919 Members | 1,587 Online
Bytes IT Community
+ Ask a Question
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

omerbutt
100+
P: 638
i have the following code through which i need to detect a season .
[code]

Expand|Select|Wrap|Line Numbers
  1. function season($period){
  2. $seasons    = array(
  3. 'spring'    => array('March 21'     , 'June 20'),
  4. 'summer'    => array('June 21'      , 'September 22'),
  5. 'fall'      => array('September 23' , 'December 20'),
  6. 'winter'    => array('December 21'  , 'March 20')
  7. );
  8.  
  9.         $seasonsYear = array();
  10.  
  11.         $start      = strtotime($period[0]);
  12.         $end        = strtotime($period[1]);
  13.  
  14.         $seasonsYear[date('Y', $start)] = array();
  15.  
  16.  
  17.         if (key(current($seasonsYear)) != date('Y', $end))
  18.             $seasonsYear[date('Y', $end)] = array();
  19.  
  20.         foreach ($seasonsYear as $year => &$seasonYear){
  21.             foreach ($seasons as $season => $period){
  22.                 $seasonYear[$season] = array(strtotime($period[0].' '.$year), strtotime($period[1].' '.($season != 'winter' && $season != 'Winter2' ? $year : ($year+1))));
  23.  
  24.             }
  25.         }
  26.  
  27.         foreach ($seasonsYear as $year => &$seasons) {
  28.             foreach ($seasons as $season => &$period) {
  29.                 if ($start >= $period[0] && $end <= $period[1]){
  30.                     return ucFirst($season);
  31.                 }
  32.  
  33.                 if ($start >= $period[0] && $start <= $period[1]) {
  34.                     if (date('Y', $end) != $year) 
  35.                         $seasons = $seasonsYear[date('Y', $end)];   
  36.                         $year = date('Y', $end);
  37.  
  38.                     $nextSeason = key($seasons);
  39.                     $nextPeriod = current($seasons);                
  40.                     do {                    
  41.                         $findNext   = ($end >= $nextPeriod[0] && $end <= $nextPeriod[1]);
  42.  
  43.                         $nextSeason = key($seasons);
  44.                         $nextPeriod = current($seasons);
  45.                     } while ($findNext = False);
  46.  
  47.                     $diffCurr   = $period[1]-$start;
  48.                     $diffNext   = $end-$nextPeriod[0];
  49.  
  50.                     if ($diffCurr > $diffNext)
  51.                         return ucFirst($season);
  52.                     else {
  53.                         return ucFirst($nextSeason);
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.     }
  59. echo season(array('2011-12-24','2011-12-26'));
  60.  
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
  1. 'summer'    => array('May 16'      , 'November 16'),
  2. 'winter'    => array('November 15'  , 'December 23'),
  3. 'christmas' => array('December 24' , 'January 1'),
  4. 'winter'    => array('January 2'  , 'May 15')
  5.  
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
Share this Question
Share on Google+
2 Replies


Dormilich
Expert Mod 5K+
P: 8,639
well, the key "winter" is used twice and there is a date overlap between summer and winter.
Nov 1 '11 #2

omerbutt
100+
P: 638
yeah dorm,
i mentioned that the key winter has been used twice , but didnt noticed the overlap in the dates and now i have changed it like

Expand|Select|Wrap|Line Numbers
  1. 'summer'    => array('May 16'      , 'November 14'),
  2. 'winter'    => array('November 15'  , 'December 23'),
  3. 'christmas' => array('December 24' , 'January 1'),
  4. 'winter2'    => array('January 2'  , 'May 15')
  5.  
it still does not work right , why is it working with the standard intervals and not with my custom intervals , unable to figure out
regards,
Omer Aslam
Nov 2 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.