Skip to content

Commit e27e214

Browse files
committed
Fixes and improvements to handle dates and date comparison, simplification of snippet with more logic moved into the Microseason class.
1 parent 8d32aa1 commit e27e214

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

classes/Microseasons.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,44 @@ public static function getSeason($today, $jsonFileUrl): array {
1717
// convert $today passed to 4-character mmdd format
1818
// since we don't care about the specific year
1919
$timestamp = Date::createFromFormat('Y-m-d', $today);
20+
$currentYear = Date::createFromFormat('Y-m-d', $today)->format('Y');
2021

21-
// create $currentSeason to hold an array of data to return
22+
$wrapper = option('scottboms.microseasons.wrapper') ?? 'div';
23+
$class = option('scottboms.microseasons.class') ?? 'microseasons';
24+
$includedates = option('scottboms.microseasons.includedates') ?? True;
25+
26+
// initialize $currentSeason to hold an array of data to return
2227
$currentSeason = array();
2328

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

2935
// adjust the start and end dates to handle year transitions
3036
if ($start > $end) {
31-
if ($timestamp >= $start || $timestamp < $end) {
37+
if ($timestamp >= $start || $timestamp <= $end) {
3238
$season['start'] = $start->format(option("scottboms.microseasons.dateformat")) ?? $start->format('M d');
3339
$season['end'] = $end->format(option("scottboms.microseasons.dateformat")) ?? $end->format('M d');
40+
$season['wrapper'] = $wrapper;
41+
$season['class'] = $class;
42+
$season['includedates'] = $includedates;
43+
$season['year'] = $currentYear;
44+
$season['start'] = $season['start'];
45+
$season['end'] = $season['end'];
3446
return $currentSeason[] = $season; // return the matching season
3547
}
3648
} else {
37-
if ($timestamp >= $start && $timestamp < $end) {
49+
if ($timestamp >= $start && $timestamp <= $end) {
3850
$season['start'] = $start->format(option("scottboms.microseasons.dateformat")) ?? $start->format('M d');
3951
$season['end'] = $end->format(option("scottboms.microseasons.dateformat")) ?? $end->format('M d');
52+
$season['wrapper'] = $wrapper;
53+
$season['class'] = $class;
54+
$season['includedates'] = $includedates;
55+
$season['year'] = $currentYear;
56+
$season['start'] = $season['start'];
57+
$season['end'] = $season['end'];
4058
return $currentSeason[] = $season; // return the matching season
4159
}
4260
}

snippets/microseasons.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
$jsonSeasons = Scottboms\Microseasons\Season::getAllSeasons();
44
$matchSeason = Scottboms\Microseasons\Season::getSeason($currentDate, $jsonSeasons);
55
?>
6-
<<?= option('scottboms.microseasons.wrapper', 'div') ?> class="<?= option('scottboms.microseasons.class', 'microseasons') ?>">
6+
<<?= $matchSeason['wrapper'] ?> class="<?= $matchSeason['class'] ?>">
77
<dl class="ms__season">
88
<dt class="ms__period"><?= $matchSeason['period'] ?></dt>
99
<dd>
1010
<span class="ms__name">
1111
<?= $matchSeason['translation'] ?>
1212
<?= $matchSeason['name'] ?>
1313
</span>
14-
<?php if(option('scottboms.microseasons.includedates') !== null && option('scottboms.microseasons.includedates') === True): ?>
14+
<?php if($matchSeason['includedates'] === True): ?>
1515
<time class="ms__range" datetime="<?= $matchSeason['start'] ?>/<?= $matchSeason['end']?>">
1616
<?= $matchSeason['start'] ?> to <?= $matchSeason['end'] ?></time>
1717
<?php endif ?>
1818
</dd>
1919
</dl>
20-
</<?= option('scottboms.microseasons.wrapper', 'div') ?>>
20+
</<?= $matchSeason['wrapper'] ?>>

0 commit comments

Comments
 (0)