Part of the EllisLab Network
   
 
get season by date
Posted: 21 March 2007 09:44 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  163
Joined  11-06-2006

Im looking all over but cant find the right code for find what season it is…

I cant find my original thread, but someone posted this solution (which doesn’t work)

$season_dates=array('/12/21'=>'winter', '/09/21'=>'autumn', '/06/21'=>'summer', '/03/20'=>'spring', '/01/01'=>'winter');
foreach (
$season_dates AS $key => $value) {
    $limit
=date("Y").$key;
    if (
time() >=strtotime($limit)) {
           $season
= $value;
    
}
}

it’s still coming up as winter… when it should be spring…

anyone have a solution?

 Signature 

“We have just gotten a wake-up call from the Nintendo Generation.” - cerealkiller; Hackers (1995)

Profile
 
 
Posted: 21 March 2007 10:14 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  240
Joined  11-10-2006

I was bored so I implemented it ...
Quick and - in my opinion - not at all dirty! wink

<?
function season($date)
{
  $year
= date("Y", $date);
  if(
$date < mktime(0, 0, 0, 3, 21, $year)) return "winter";
  if(
$date < mktime(0, 0, 0, 6, 21, $year)) return "spring";
  if(
$date < mktime(0, 0, 0, 9, 21, $year)) return "summer";
  if(
$date < mktime(0, 0, 0, 12, 21, $year)) return "autumn";
  return
"winter";
}
?>

Expectes a timestamp, but if you want e.g. a MySQL date, adaption should be too difficult (my MySQL DATETIME Helper may be useful in this case).

Tested it with the following dates:

<?
$seasons
= array();
$seasons[] = season(mktime(23, 59, 59, 3, 20, 2007)); # march 20 -> still winter
$seasons[] = season(mktime(0, 0, 0, 3, 21, 2007)); # march 21 -> spring
$seasons[] = season(mktime(23, 59, 59, 6, 20, 2007)); # june 20 -> still spring
$seasons[] = season(mktime(0, 0, 0, 6, 21, 2007)); # june 21 -> summer
$seasons[] = season(mktime(23, 59, 59, 9, 20, 2007)); # september 20 -> still summer
$seasons[] = season(mktime(0, 0, 0, 9, 21, 2007)); # september 21 -> autumn
$seasons[] = season(mktime(23, 59, 59, 12, 20, 2007)); # december 20 -> still autumn
$seasons[] = season(mktime(0, 0, 0, 12, 21, 2007)); # december 21 -> winter
$seasons[] = season(mktime(0, 0, 0, 12, 24, 2007)); # merry christmas :)
?>
<pre>
<? print_r($seasons); ?>
</pre>

Output seems to be correct, so it should work! wink

Have fun wink

 Signature 

blog

Profile
 
 
Posted: 21 March 2007 12:38 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  886
Joined  03-06-2006
Clemens- - 21 March 2007 10:14 AM

I was bored so I implemented it ...
Quick and - in my opinion - not at all dirty! wink

Assuming that using the 21st is accurate enough, it looks fine.  Of course, actual equinoxes and solstices vary by several days, so if one needs greater accuracy it gets sticky.  If you really want to herald the changing of the seasons, you even need to decide whether UTC is good enough, or whether you need local standard time (and what about daylight savings) or if you need real blood-and-guts astronomical time.

Probably it’s better just to use Clemens’ code and forget I said anything wink

 Signature 

Corozal, Belize | Linux.bz | Using Kubuntu Linux 7.10 | CodeIgniter 1.5.3

Profile
 
 
Posted: 21 March 2007 01:01 PM   [ Ignore ]   [ # 3 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  163
Joined  11-06-2006

lol.  Code worked great thank you.

 Signature 

“We have just gotten a wake-up call from the Nintendo Generation.” - cerealkiller; Hackers (1995)

Profile
 
 
Posted: 21 March 2007 08:58 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  391
Joined  02-25-2007
cnurv - 21 March 2007 09:44 AM

Im looking all over but cant find the right code for find what season it is…

I cant find my original thread, but someone posted this solution (which doesn’t work)

Doesn’t work?  It works just fine.  Here is the code I took the time to find, modify and post for you:

function season() {

    $season_dates
= array(
    
'/12/21'=>'Winter',
    
'/09/21'=>'Autumn',
    
'/06/21'=>'Summer',
    
'/03/21'=>'Spring',
    
'/01/01'=>'Winter');

    foreach (
$season_dates AS $key => $value) {
        
        $limit
= date("Y").$key;
        
        if (
time() >= strtotime($limit)) {
            
            
return $value;
            
        
}
    }
}

echo season();

Please don’t smear me by saying my contributions “don’t work”.  I think maybe a correct statement would be that you couldn’t get it to work?  Did you understand that what I posted was a function?  The snipped you posted here in this thread isn’t the complete function.

Maybe I’m just a little pissy tonight and taking this too personally - but still, I CHOOSE to come here and try and help where I can, just like the other contributors here.

 Signature 

InkType.org - CodeIgniter Based Blogging Software | CodeIgniter 101: Models | AutoModel

Profile
 
 
Posted: 22 March 2007 08:06 AM   [ Ignore ]   [ # 5 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  163
Joined  11-06-2006

by saying it didn’t work was not a personal attack to you.  I’m sorry if you are having a bad night.  I know it was a function, I just didn’t post the function statement around it.  For some reason it still came up as winter? not sure why, Thank you for your contribution.

 Signature 

“We have just gotten a wake-up call from the Nintendo Generation.” - cerealkiller; Hackers (1995)

Profile
 
 
Posted: 22 March 2007 10:35 AM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  886
Joined  03-06-2006

@Developer13—I noticed one problem that might have led to your code working for you and not for cnurv.  As I understand it, you cannot count on PHP storing associative array keys in the order they were entered.  Different versions of PHP might handle it differently.  However, there is a simple solution ... if you want the keys to be sorted, use ksort(), or in this case krsort() to be sure the keys are sorted the way you want.  In your code:

krsort($season_dates) // <-- add this line
foreach ($season_dates AS $key => $value) {

I didn’t test either code, but with the above addition it looks like either one should work.  And remember that others besides cnurv might find one or the other useful, so no CI soldiers efforts have been “wasted” in this conflict.
wink

 Signature 

Corozal, Belize | Linux.bz | Using Kubuntu Linux 7.10 | CodeIgniter 1.5.3

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 64457 Total Logged-in Users: 15
Total Topics: 80967 Total Anonymous Users: 0
Total Replies: 435700 Total Guests: 174
Total Posts: 516667    
Members ( View Memberlist )