Created
September 20, 2012 11:52
-
-
Save netsensei/3755467 to your computer and use it in GitHub Desktop.
Deduces the season based on a given date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_season($hemisphere = 'northern', $date = '') { | |
$now = (!empty($date)) ? strtotime($date) : strtotime(date('Y/m/d')); | |
$season_dates = array( | |
'northern' => array( | |
'/03/21' => 'spring', | |
'/06/21' => 'summer', | |
'/09/21' => 'autumn', | |
'/12/21' => 'winter', | |
), | |
'soutern' => array( | |
'/03/21' => 'autumn', | |
'/06/21' => 'winter', | |
'/09/21' => 'spring', | |
'/12/21' => 'summer', | |
), | |
); | |
$previous = strtotime(date('Y') . '01/01'); | |
$previous_season = $result = 'winter'; | |
foreach ($season_dates[$hemisphere] as $key => $value) { | |
$start = $previous; | |
$end = strtotime(date('Y') . $key); | |
if ($now >= $start && $now < $end) { | |
$date = date('Y') . $key; | |
$result = $previous_season; | |
} | |
$previous_season = $value; | |
$previous = $end; | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment