Skip to content

Commit

Permalink
Fixes and improvements to handle dates and date comparison, simplific…
Browse files Browse the repository at this point in the history
…ation of snippet with more logic moved into the Microseason class.
  • Loading branch information
scottboms committed Feb 15, 2024
1 parent 8d32aa1 commit e27e214
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
28 changes: 23 additions & 5 deletions classes/Microseasons.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,44 @@ public static function getSeason($today, $jsonFileUrl): array {
// convert $today passed to 4-character mmdd format
// since we don't care about the specific year
$timestamp = Date::createFromFormat('Y-m-d', $today);
$currentYear = Date::createFromFormat('Y-m-d', $today)->format('Y');

// create $currentSeason to hold an array of data to return
$wrapper = option('scottboms.microseasons.wrapper') ?? 'div';
$class = option('scottboms.microseasons.class') ?? 'microseasons';
$includedates = option('scottboms.microseasons.includedates') ?? True;

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

// check which season the current date falls within
foreach ($seasonsArray as $season) {
$start = Date::createFromFormat('Y-m-d', $season["start"]);
$end = Date::createFromFormat('Y-m-d', $season["end"]);
// handle updating the raw dates from the json values
$start = Date::createFromFormat('Y-m-d', $currentYear . '-' . substr($season["start"], 5));
$end = Date::createFromFormat('Y-m-d', $currentYear . '-' . substr($season["end"], 5));

// adjust the start and end dates to handle year transitions
if ($start > $end) {
if ($timestamp >= $start || $timestamp < $end) {
if ($timestamp >= $start || $timestamp <= $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');
$season['wrapper'] = $wrapper;
$season['class'] = $class;
$season['includedates'] = $includedates;
$season['year'] = $currentYear;
$season['start'] = $season['start'];
$season['end'] = $season['end'];
return $currentSeason[] = $season; // return the matching season
}
} else {
if ($timestamp >= $start && $timestamp < $end) {
if ($timestamp >= $start && $timestamp <= $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');
$season['wrapper'] = $wrapper;
$season['class'] = $class;
$season['includedates'] = $includedates;
$season['year'] = $currentYear;
$season['start'] = $season['start'];
$season['end'] = $season['end'];
return $currentSeason[] = $season; // return the matching season
}
}
Expand Down
6 changes: 3 additions & 3 deletions snippets/microseasons.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
$jsonSeasons = Scottboms\Microseasons\Season::getAllSeasons();
$matchSeason = Scottboms\Microseasons\Season::getSeason($currentDate, $jsonSeasons);
?>
<<?= option('scottboms.microseasons.wrapper', 'div') ?> class="<?= option('scottboms.microseasons.class', 'microseasons') ?>">
<<?= $matchSeason['wrapper'] ?> class="<?= $matchSeason['class'] ?>">
<dl class="ms__season">
<dt class="ms__period"><?= $matchSeason['period'] ?></dt>
<dd>
<span class="ms__name">
<?= $matchSeason['translation'] ?>
<?= $matchSeason['name'] ?>
</span>
<?php if(option('scottboms.microseasons.includedates') !== null && option('scottboms.microseasons.includedates') === True): ?>
<?php if($matchSeason['includedates'] === True): ?>
<time class="ms__range" datetime="<?= $matchSeason['start'] ?>/<?= $matchSeason['end']?>">
<?= $matchSeason['start'] ?> to <?= $matchSeason['end'] ?></time>
<?php endif ?>
</dd>
</dl>
</<?= option('scottboms.microseasons.wrapper', 'div') ?>>
</<?= $matchSeason['wrapper'] ?>>

0 comments on commit e27e214

Please sign in to comment.