Skip to content

Commit

Permalink
More bug fixes to get this mostly working. Still need to resolve how …
Browse files Browse the repository at this point in the history
…to handle rolling between years overall.
  • Loading branch information
scottboms committed Feb 14, 2024
1 parent 78b3e44 commit 8d32aa1
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 157 deletions.
22 changes: 10 additions & 12 deletions classes/Microseasons.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,38 @@ public static function getAllSeasons(): string {
return __DIR__ . '/../microseasons.json';
}

public static function getSeason($currentDate, $jsonFileUrl): array {
public static function getSeason($today, $jsonFileUrl): array {
// fetch and decode the JSON data from the external link
$json = file_get_contents($jsonFileUrl);
$seasonsArray = json_decode($json, true);

// convert $currentDate passed to 4-character mmdd format
// convert $today passed to 4-character mmdd format
// since we don't care about the specific year
$timestamp = date("m-d", strtotime($currentDate));
$timestamp = Date::createFromFormat('Y-m-d', $today);

// create $currentSeason to hold an array of data to return
$currentSeason = array();

// check which season the current date falls within
foreach ($seasonsArray as $season) {
$start = $season["start"];
$end = $season["end"];
$start = Date::createFromFormat('Y-m-d', $season["start"]);
$end = Date::createFromFormat('Y-m-d', $season["end"]);

// adjust the start and end dates to handle year transitions
if ($start > $end) {
if ($timestamp >= $start || $timestamp < $end) {
// convert the date information to dates object
$season["start"] = Date::createFromFormat('m-d', $start);
$season["end"] = Date::createFromFormat('m-d', $end);
$season['start'] = $start->format(option("scottboms.microseasons.dateformat")) ?? $start->format('M d');
$season['end'] = $end->format(option("scottboms.microseasons.dateformat")) ?? $end->format('M d');
return $currentSeason[] = $season; // return the matching season
}
} else {
if ($timestamp >= $start && $timestamp < $end) {
$season["start"] = Date::createFromFormat('m-d', $start);
$season["end"] = Date::createFromFormat('m-d', $end);
$season['start'] = $start->format(option("scottboms.microseasons.dateformat")) ?? $start->format('M d');
$season['end'] = $end->format(option("scottboms.microseasons.dateformat")) ?? $end->format('M d');
return $currentSeason[] = $season; // return the matching season
}
}
}
return $currentSeason; // no match fallback
return $currentSeason;
}

}
Loading

0 comments on commit 8d32aa1

Please sign in to comment.