473,320 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Detect Seasons with a given date interval

omerbutt
638 512MB
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
2 2100
Dormilich
8,658 Expert Mod 8TB
well, the key "winter" is used twice and there is a date overlap between summer and winter.
Nov 1 '11 #2
omerbutt
638 512MB
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

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

Similar topics

2
by: MONO | last post by:
Hello! I´m trying to get the weeknumber fom a given date but at some dates will return the wrong weeknumber. WeekNumber = DatePart("ww", TodayAsDate, vbMonday, vbFirstFourDays) like...
4
by: Lynn | last post by:
On a form I have Date_Start Date_End I have a new Date_Start1 Date_End1 which the use inputs. I need to validate that Date_Start1 and...
1
by: Dave | last post by:
I have a problem that I can't quite get started on solving. I have a table of asset statuses. Each time the asset status changes, a new row is inserted into the table. CREATE TABLE...
2
by: jsfromynr | last post by:
Hi all, I have two tables CREATE TABLE ( NOT NULL , NULL , -- CURRENT DESIGNATION OF EMPLOYEE NOT NULL ) ON GO
8
by: dlx_son | last post by:
Here is the code so far <form name="thisform"> <h3>Enter time to add to or subtract from:</h3> (If not entered, current time will be used)<br> Day: <input name="d1" alt="Day of month"...
3
by: UnixSlaxer | last post by:
Hello All, While doing some TPC benchmark testing on DB2-UDB 8.2, I face the following problem when running this query: select * from lineitem where l_shipdate <= date '1998-12-01' -...
4
by: perryclisbee via AccessMonster.com | last post by:
I have dates of service for several people that range all over each month. ie: patient had dates of service of: 7/3/2006, 7/24/2006 and 7/25/2006. I need to create a new field via a query that...
1
by: oyis | last post by:
Hi, I'm looking for a function,which finds week number of given date. I found this method before,but I can't find now... Please help me...
1
by: aneinander | last post by:
I'm beginner and getting following error message, please help. The table contains transaction date, account number, branch, etc. And need to show previous transaction date or next transaction date...
1
by: abdutasleem357 | last post by:
i want to know the query in oracle for, whether the given date is present or available in between two dates(startdate, enddate)
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.