udexter
udexter

Reputation: 2347

Giving a two dates (or two months) detect in what season we are in PHP

I would like to know if there is some easy way to identify seasons: spring, summer, autumn or winter. I'm have to generate a 'resume' and I'd like that if a period of working roughly fits a season (not exactly but with a +/-10 days error for example) it returns spring, summer, autumn or winter.

Like:

(In Spain summer is between 21 July and 20 September)

Any idea how to do this?

Upvotes: 1

Views: 2612

Answers (3)

Fy-
Fy-

Reputation: 353

As you are looking for a season for a period I quickly wrote this function, it can be improve and may have some bugs but you have somewhere to start.

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' ? $year : ($year+1))));

    foreach ($seasonsYear as $year => &$seasons) {
        foreach ($seasons as $season => &$period) {
            if ($start >= $period[0] && $end <= $period[1])
                return ucFirst($season).' '.$year;

            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).' '.$year;
                else {
                    return ucFirst($nextSeason).' '.$year;
                }
            }
        }
    }
}

echo season(array('07/20/2010', '08/20/2010'));
echo "\n";
echo season(array('06/25/2010', '09/30/2010'));
echo "\n";
echo season(array('08/25/2010', '11/30/2010'));
echo "\n";
echo season(array('12/21/2010', '01/01/2011'));
echo "\n";
echo season(array('12/21/2010', '03/25/2011'));

Result:

/*
Summer 2010
Summer 2010
Fall 2010
Winter 2010
Winter 2011
*/

And the except you want for "season year overflow":

if (date('Y', $end) != $year) 
    return $year.'-'.date('Y', $end);

Instead of:

if (date('Y', $end) != $year) 
    $seasons = $seasonsYear[date('Y', $end)];   
    $year = date('Y', $end);

Note: Winter is coming.

Upvotes: 2

Tom
Tom

Reputation: 1701

Looks like this guy got that function written already : http://biostall.com/get-the-current-season-using-php

Even has hemisphere support !

But this should do the trick :

<?php
    function getSeason($date) {
        $season_names = array('Winter', 'Spring', 'Summer', 'Fall');  
        if (strtotime($date) < strtotime($date_year.'-03-21') || strtotime($date) >= strtotime($date_year.'-12-21')) {  
            return $season_names[0]; // Must be in Winter
        } elseif (strtotime($date) >= strtotime($date_year.'-09-23')) {  
            return $season_names[3]; // Must be in Fall  
        } elseif (strtotime($date) >= strtotime($date_year.'-06-21')) {  
            return $season_names[2]; // Must be in Summer  
        } elseif (strtotime($date) >= strtotime($date_year.'-03-21')) {  
            return $season_names[1]; // Must be in Spring  
        }  
    }

Upvotes: 2

fredley
fredley

Reputation: 33901

Here's a rough overview of what you need to do:

  • Work out when your season boundarys are going to be. This is a big, big job if you're going to do this for an international scope!

  • When presented with a date range, first work out exactly how many days of that range are in each season.

You want output like:

 Range | Days | complete?
  Su10 |  12  |     0
   A10 |  90  |     1
   W10 |  02  |     0
  • Once this is done you'll need to identify if a single season is 'complete', which should be 1 if the whole season is worked, within 10 days. If it is, choose that season. If more are complete, or none, return false.

Upvotes: 1

Related Questions