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
0 comments:
Post a Comment