Tuesday 17 July 2018

Get the Current Season Using PHP

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:

  1. // $date - A date in any English textual format. If blank   
  2. // defaults to the current date  
  3. // $hemisphere - "northern", "southern" or "australia"  
  4. function get_season($date=""$hemisphere="northern") {  
  5.       
  6.     // Set $date to today if no date specified  
  7.     if ($date=="") { $date = date("Y-m-d"); }  
  8.       
  9.     // Specify the season names  
  10.     $season_names = array('Winter''Spring''Summer''Fall');  
  11.   
  12.     // Get year of date specified  
  13.     $date_year = date("Y"strtotime($date));  
  14.   
  15.     // Declare season date ranges  
  16.     switch (strtolower($hemisphere)) {  
  17.         case "northern": {  
  18.             if (  
  19.                 strtotime($date)<strtotime($date_year.'-03-21') ||   
  20.                 strtotime($date)>=strtotime($date_year.'-12-21')  
  21.             ) {   
  22.                 return $season_names[0]; // Must be in Winter  
  23.             }elseif (strtotime($date)>=strtotime($date_year.'-09-23')) {  
  24.                 return $season_names[3]; // Must be in Fall  
  25.             }elseif (strtotime($date)>=strtotime($date_year.'-06-21')) {  
  26.                 return $season_names[2]; // Must be in Summer  
  27.             }elseif (strtotime($date)>=strtotime($date_year.'-03-21')) {  
  28.                 return $season_names[1]; // Must be in Spring  
  29.             }  
  30.             break;  
  31.         }  
  32.         case "southern": {  
  33.             if (  
  34.                 strtotime($date)<strtotime($date_year.'-03-21') ||   
  35.                 strtotime($date)>=strtotime($date_year.'-12-21')  
  36.             ) {   
  37.                 return $season_names[2]; // Must be in Summer  
  38.             }elseif (strtotime($date)>=strtotime($date_year.'-09-23')) {  
  39.                 return $season_names[1]; // Must be in Spring  
  40.             }elseif (strtotime($date)>=strtotime($date_year.'-06-21')) {  
  41.                 return $season_names[0]; // Must be in Winter  
  42.             }elseif (strtotime($date)>=strtotime($date_year.'-03-21')) {  
  43.                 return $season_names[3]; // Must be in Fall   
  44.             }  
  45.             break;  
  46.         }  
  47.         case "australia": {  
  48.             if (  
  49.                 strtotime($date)<strtotime($date_year.'-03-01') ||   
  50.                 strtotime($date)>=strtotime($date_year.'-12-01')  
  51.             ) {   
  52.                 return $season_names[2]; // Must be in Summer  
  53.             }elseif (strtotime($date)>=strtotime($date_year.'-09-01')) {  
  54.                 return $season_names[1]; // Must be in Spring  
  55.             }elseif (strtotime($date)>=strtotime($date_year.'-06-01')) {  
  56.                 return $season_names[0]; // Must be in Winter  
  57.             }elseif (strtotime($date)>=strtotime($date_year.'-03-01')) {  
  58.                 return $season_names[3]; // Must be in Fall   
  59.             }  
  60.             break;  
  61.         }  
  62.         default: { echo "Invalid hemisphere set"; }  
  63.     }  
  64.   
  65. }  
Examples
Using the function above we can then call it in a variety of ways:

  1. echo get_season("March 9th 2010"); // returns Winter  
  2.   
  3. echo get_season("2010-03-09""southern"); // returns Summer  
  4.   
  5. 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