Skip to content

Instantly share code, notes, and snippets.

@netsensei
Created September 20, 2012 11:52
Show Gist options
  • Save netsensei/3755467 to your computer and use it in GitHub Desktop.
Save netsensei/3755467 to your computer and use it in GitHub Desktop.
Deduces the season based on a given date
<?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