From 8cb5fb086886f2dd67b5568837d51f4ff5790b45 Mon Sep 17 00:00:00 2001 From: Artyom Mezin Date: Wed, 15 Jun 2016 11:29:50 +1100 Subject: [PATCH 1/2] Remove end date check if not specified (better 32-bit support) Related to https://github.com/getgrav/grav/issues/898 I can't find better way to remove it with DRY principle :expressionless: With smaller code we need to check a value for each `$this->items` iteration. Also it can be used with `PHP_INT_MAX` with previous version of this code. --- system/src/Grav/Common/Page/Collection.php | 27 ++++++++++++++++------ 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/system/src/Grav/Common/Page/Collection.php b/system/src/Grav/Common/Page/Collection.php index f21a58380..4f2ad3421 100644 --- a/system/src/Grav/Common/Page/Collection.php +++ b/system/src/Grav/Common/Page/Collection.php @@ -270,17 +270,30 @@ public function currentPosition($path) public function dateRange($startDate, $endDate = false, $field = false) { $start = Utils::date2timestamp($startDate); - $end = $endDate ? Utils::date2timestamp($endDate) : strtotime("now +1000 years"); + $end = $endDate ? Utils::date2timestamp($endDate) : false; $date_range = []; - foreach ($this->items as $path => $slug) { - $page = $this->pages->get($path); - if ($page !== null) { - $date = $field ? strtotime($page->value($field)) : $page->date(); + if ($end) { + foreach ($this->items as $path => $slug) { + $page = $this->pages->get($path); + if ($page !== null) { + $date = $field ? strtotime($page->value($field)) : $page->date(); + + if ($date > $start && $date < $end) { + $date_range[$path] = $slug; + } + } + } + } else { + foreach ($this->items as $path => $slug) { + $page = $this->pages->get($path); + if ($page !== null) { + $date = $field ? strtotime($page->value($field)) : $page->date(); - if ($date > $start && $date < $end) { - $date_range[$path] = $slug; + if ($date > $start) { + $date_range[$path] = $slug; + } } } } From ba812efaca0d61f64a3b751d6fc466bae546f02a Mon Sep 17 00:00:00 2001 From: Artyom Mezin Date: Wed, 15 Jun 2016 11:44:26 +1100 Subject: [PATCH 2/2] Include endpoints --- system/src/Grav/Common/Page/Collection.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/src/Grav/Common/Page/Collection.php b/system/src/Grav/Common/Page/Collection.php index 4f2ad3421..46f1891e8 100644 --- a/system/src/Grav/Common/Page/Collection.php +++ b/system/src/Grav/Common/Page/Collection.php @@ -280,7 +280,7 @@ public function dateRange($startDate, $endDate = false, $field = false) if ($page !== null) { $date = $field ? strtotime($page->value($field)) : $page->date(); - if ($date > $start && $date < $end) { + if ($date >= $start && $date <= $end) { $date_range[$path] = $slug; } } @@ -291,7 +291,7 @@ public function dateRange($startDate, $endDate = false, $field = false) if ($page !== null) { $date = $field ? strtotime($page->value($field)) : $page->date(); - if ($date > $start) { + if ($date >= $start) { $date_range[$path] = $slug; } }