PHPRO.ORG

The PHP date functions and datetime class provide a multitude of date information and utilities to manipulate dates and times. Most of these are based on GMT or shifts east to west. However, the four seasons are hemisphere specific and so the date and datetime functions become redundant. As there are only two hemispheres an array of months for each hemisphere can be used and a month value supplied to check and a match found where the month occurs in the array.

The function below shows how to get the season for any date and hemisphere.


<?php

/**
 *
 * @get the season
 *
 * @param string $hemisphere
 *
 * @param int $month
 *
 * @return string
 *
 */
function getSeason($hemisphere$month=null)
{
    
$month is_null($month) ? date('m') : $month;

    
/*** southern hemisphere seasons ***/
    
$southern=array(
    
'summer' => array(1212),
    
'autumn' => array(345),
    
'winter' => array(678),
    
'spring' => array(91011)
    );

    
/*** northern hemisphere seasons ***/
    
$northern=array(
    
'summer' => array(678),
    
'autumn' => array(91011),
    
'winter' => array(1212),
    
'spring' => array(345)
    );

    
/*** loop over the hemisphere ***/
    
foreach($$hemisphere as $key=>$val)
    {
        if(
in_array($month$val))
        {
            return 
$key;
        }
    }
    return 
false;
}

/*** show season of december in the northern hemisphere  ***/
echo getSeason('northern'12);

?>