Created
July 1, 2022 21:12
-
-
Save rmcfadzean/ad28edf137b8d0b9373ccffd21d16917 to your computer and use it in GitHub Desktop.
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
| <? | |
| /** | |
| * Takes a date, returns the meteorological season of that date, by month. Not equinox/solstice. | |
| * Only works for the northern hemisphere. | |
| * | |
| * @link https://en.wikipedia.org/wiki/Season#Meteorological | |
| * @param string $date | |
| * @return string | |
| **/ | |
| function get_season_for_date(string $date) | |
| { | |
| if (is_null($date)) { | |
| return false; | |
| } | |
| $date = new DateTime($date); | |
| $month = intval($date->format("m")); | |
| $season_ranges = [ | |
| "spring" => [3, 4, 5], | |
| "summer" => [6, 7, 8], | |
| "autumn" => [9, 10, 11], | |
| "winter" => [12, 1, 2] | |
| ]; | |
| $result = array_reduce(array_keys($season_ranges), function ($carry, $key) use ($season_ranges, $month) { | |
| $curr = $season_ranges[$key]; | |
| $is_season = in_array($month, $curr); | |
| if ($is_season) { | |
| $carry = $key; | |
| } | |
| return $carry; | |
| }); | |
| return $result; | |
| } | |
| echo get_season_for_date("june 21st"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment