From f67bb675a132695fbe20bee608aba5b14cf94c88 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 2 Sep 2015 15:29:41 -0600 Subject: [PATCH] Added support for custom page date format: https://github.com/getgrav/grav-plugin-admin/issues/135 --- system/blueprints/config/system.yaml | 7 +++++++ system/config/system.yaml | 1 + system/src/Grav/Common/Page/Collection.php | 5 +++-- system/src/Grav/Common/Page/Page.php | 12 ++++++------ system/src/Grav/Common/Utils.php | 20 ++++++++++++++++++++ 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/system/blueprints/config/system.yaml b/system/blueprints/config/system.yaml index b7f9e683d..cdfa23fd9 100644 --- a/system/blueprints/config/system.yaml +++ b/system/blueprints/config/system.yaml @@ -49,6 +49,13 @@ form: options: '': 'Default (Server Timezone)' + pages.dateformat.default: + type: text + size: medium + label: PLUGIN_ADMIN.DEFAULT_DATE_FORMAT + help: PLUGIN_ADMIN.DEFAULT_DATE_FORMAT_HELP + placeholder: PLUGIN_ADMIN.DEFAULT_DATE_FORMAT_PLACEHOLDER + pages.dateformat.short: type: dateformat size: medium diff --git a/system/config/system.yaml b/system/config/system.yaml index 9b3bbc4dd..d0946a7a9 100644 --- a/system/config/system.yaml +++ b/system/config/system.yaml @@ -23,6 +23,7 @@ pages: list: count: 20 # Default item count per page dateformat: + default: # The default date format Grav expects in the `date: ` field short: 'jS M Y' # Short date format long: 'F jS \a\t g:ia' # Long date format publish_dates: true # automatically publish/unpublish based on dates diff --git a/system/src/Grav/Common/Page/Collection.php b/system/src/Grav/Common/Page/Collection.php index 029788f2f..323fb9412 100644 --- a/system/src/Grav/Common/Page/Collection.php +++ b/system/src/Grav/Common/Page/Collection.php @@ -3,6 +3,7 @@ namespace Grav\Common\Page; use Grav\Common\Grav; use Grav\Common\Iterator; +use Grav\Common\Utils; /** * Collection of Pages. @@ -225,8 +226,8 @@ class Collection extends Iterator */ public function dateRange($startDate, $endDate = false, $field = false) { - $start = strtotime($startDate); - $end = $endDate ? strtotime($endDate) : strtotime("now +1000 years"); + $start = Utils::date2timestamp($startDate); + $end = $endDate ? Utils::date2timestamp($endDate) : strtotime("now +1000 years"); $date_range = []; diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 5f88af978..8983cfd5d 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -305,7 +305,7 @@ class Page $this->order_manual = (array)$this->header->order_manual; } if (isset($this->header->date)) { - $this->date = strtotime($this->header->date); + $this->date($this->header->date); } if (isset($this->header->markdown_extra)) { $this->markdown_extra = (bool)$this->header->markdown_extra; @@ -327,10 +327,10 @@ class Page $this->published = (bool) $this->header->published; } if (isset($this->header->publish_date)) { - $this->publish_date = strtotime($this->header->publish_date); + $this->publishDate($this->header->publish_date); } if (isset($this->header->unpublish_date)) { - $this->unpublish_date = strtotime($this->header->unpublish_date); + $this->unpublishDate($this->header->unpublish_date); } if (isset($this->header->expires)) { $this->expires = intval($this->header->expires); @@ -1051,7 +1051,7 @@ class Page public function publishDate($var = null) { if ($var !== null) { - $this->publish_date = strtotime($var); + $this->publish_date = Utils::date2timestamp($var); } if ($this->publish_date === null) { @@ -1070,7 +1070,7 @@ class Page public function unpublishDate($var = null) { if ($var !== null) { - $this->unpublish_date = strtotime($var); + $this->unpublish_date = Utils::date2timestamp($var); } return $this->unpublish_date; @@ -1487,7 +1487,7 @@ class Page public function date($var = null) { if ($var !== null) { - $this->date = strtotime($var); + $this->date = Utils::date2timestamp($var); } if (!$this->date) { diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index d7421c9b6..171c181f6 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -1,6 +1,8 @@ get('system.pages.dateformat.default'); + // try to use DateTime and default format + if ($default_dateformat) { + $datetime = DateTime::createFromFormat($default_dateformat, $date); + } else { + $datetime = new DateTime($date); + } + + // fallback to strtotime if DateTime approach failed + if ($datetime !== false) { + return $datetime->getTimestamp(); + } else { + return strtotime($date); + } + } }