Get the Current Season Using PHP

October 4th, 2010 - Posted by Steve Marks to PHP, Web Development.

Get the current season using PHP

The PHP date() function is great for the majority of tasks regarding getting a date or time, however one thing it doesn’t cater for is obtaining the season. Seasons generally vary depending on the year and where you’re based in the world; While it’s Summer in the Northern Hemisphere it’s Winter in Australia.

The Code

The PHP function I’ve created below allows you to specify the date in question and also the location (Northern Hemisphere, Southern Hemisphere or Australia) and returns the correct season:

// $date - A date in any English textual format. If blank 
// defaults to the current date
// $hemisphere - "northern", "southern" or "australia"
function get_season($date="", $hemisphere="northern") {
	
	// Set $date to today if no date specified
	if ($date=="") { $date = date("Y-m-d"); }
	
	// Specify the season names
	$season_names = array('Winter', 'Spring', 'Summer', 'Fall');

	// Get year of date specified
	$date_year = date("Y", strtotime($date));

	// Declare season date ranges
	switch (strtolower($hemisphere)) {
		case "northern": {
			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
			}
			break;
		}
		case "southern": {
			if (
				strtotime($date)<strtotime($date_year.'-03-21') || 
				strtotime($date)>=strtotime($date_year.'-12-21')
			) { 
				return $season_names[2]; // Must be in Summer
			}elseif (strtotime($date)>=strtotime($date_year.'-09-23')) {
				return $season_names[1]; // Must be in Spring
			}elseif (strtotime($date)>=strtotime($date_year.'-06-21')) {
				return $season_names[0]; // Must be in Winter
			}elseif (strtotime($date)>=strtotime($date_year.'-03-21')) {
				return $season_names[3]; // Must be in Fall	
			}
			break;
		}
		case "australia": {
			if (
				strtotime($date)<strtotime($date_year.'-03-01') || 
				strtotime($date)>=strtotime($date_year.'-12-01')
			) { 
				return $season_names[2]; // Must be in Summer
			}elseif (strtotime($date)>=strtotime($date_year.'-09-01')) {
				return $season_names[1]; // Must be in Spring
			}elseif (strtotime($date)>=strtotime($date_year.'-06-01')) {
				return $season_names[0]; // Must be in Winter
			}elseif (strtotime($date)>=strtotime($date_year.'-03-01')) {
				return $season_names[3]; // Must be in Fall	
			}
			break;
		}
		default: { echo "Invalid hemisphere set"; }
	}

}

Examples

Using the function above we can then call it in a variety of ways:

echo get_season("March 9th 2010"); // returns Winter

echo get_season("2010-03-09", "southern"); // returns Summer

echo get_season("9 Mar 2010", "australia"); // returns Fall

Please note that seasons may vary by a day either way every few years

Tags: ,
This entry was posted on Monday, October 4th, 2010 at 10:49 pm by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.
Comments...

Fear not, we won't publish this

Comments (5)
  1. @Federico – You are correct in that the solstice dates do change year on year. That said, if you look at the solstice dates for the future, about 80% of the time the dates do fall on the dates specified in the function.

    For example, between the years 2010 and 2019, the Summer Solstice is on June 21st for 8 of the 10 years.

    Without reading the solstice dates from a live feed or pre-programming these dates in, I don’t see how you can get more accurate than this by using a function similar to this. Unless you have any ideas I could implement?

  2. Federico says:

    The function is wrong:
    summer depend from solstice, and this one change from year to year..

    Take a look at
    https://en.wikipedia.org/wiki/Solstice

  3. Derrick says:

    Nice! Came in handy, thanks.

  4. Alain says:

    Thank you, very useful!

  5. cujo says:

    This was really helpful. Thank you!