I thought that getting the current year's season would be something easy but after a lot of searching i only found online solutions for the current year e.g. 2020, 2019 etc.
Of course, this was not the best solution because i would have to remember each day to update this function. This is my solution.
function get_current_year_season() {
$seasons = [
'winter' => [
'start' => '01-12',
'end' => '28-02',
'months' => ['12','01','02'],
],
'spring' => [
'start' => '01-03',
'end' => '31-05',
'months' => ['03','04','05'],
],
'summer' => [
'start' => '01-06',
'end' => '31-08',
'months' => ['06','07','08'],
],
'fall' => [
'start' => '01-09',
'end' => '30-11',
'months' => ['09','10','11'],
]
];
$today = new \DateTime();
$current_month = $today->format('m');
//Check if the year is a leap year (29th of Feb)
$leap_year_date = '29-02-'.$today->format('Y');
$is_leap_year = new \Datetime($leap_year_date);
if ($leap_year_date === $is_leap_year->format('d-m-Y')) {
$seasons['winter']['end'] = '29-02';
}
foreach ($seasons as $season_name => $season) {
if (in_array($current_month,$season['months'])) {
return [
'season' => $season_name,
'period' => [
'start' => new \DateTime($season['start'].'-'.$today->format('Y')),
'end' => new \DateTime($season['end'].'-'.$today->format('Y')),
]
];
}
}
}