From 7a1d9e454b4386c9758cfbb5f997adab4bef366c Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Fri, 12 Feb 2016 15:33:23 -0700 Subject: [PATCH] Add support for custom page-level dateformat field (core only) --- system/src/Grav/Common/Page/Page.php | 27 ++++++++++++++++++++++++--- system/src/Grav/Common/Utils.php | 12 ++++++------ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index b9b6db7b8..c0f581fa7 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -75,6 +75,7 @@ class Page protected $max_count; protected $menu; protected $date; + protected $dateformat; protected $taxonomy; protected $order_by; protected $order_dir; @@ -349,6 +350,9 @@ class Page if (isset($this->header->order_manual)) { $this->order_manual = (array)$this->header->order_manual; } + if (isset($this->header->dateformat)) { + $this->dateformat($this->header->dateformat); + } if (isset($this->header->date)) { $this->date($this->header->date); } @@ -1208,7 +1212,7 @@ class Page public function publishDate($var = null) { if ($var !== null) { - $this->publish_date = Utils::date2timestamp($var); + $this->publish_date = Utils::date2timestamp($var, $this->dateformat); } return $this->publish_date; @@ -1224,7 +1228,7 @@ class Page public function unpublishDate($var = null) { if ($var !== null) { - $this->unpublish_date = Utils::date2timestamp($var); + $this->unpublish_date = Utils::date2timestamp($var, $this->dateformat); } return $this->unpublish_date; @@ -1744,7 +1748,7 @@ class Page public function date($var = null) { if ($var !== null) { - $this->date = Utils::date2timestamp($var); + $this->date = Utils::date2timestamp($var, $this->dateformat); } if (!$this->date) { @@ -1754,6 +1758,23 @@ class Page return $this->date; } + /** + * Gets and sets the date format for this Page object. This is typically passed in via the page headers + * using typical PHP date string structure - http://php.net/manual/en/function.date.php + * + * @param string $var string representation of a date format + * + * @return string string representation of a date format + */ + public function dateformat($var = null) + { + if ($var !== null) { + $this->dateformat = $var; + } + + return $this->dateformat; + } + /** * Gets and sets the order by which any sub-pages should be sorted. * diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index 85702c3e7..e812ba91a 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -122,7 +122,7 @@ abstract class Utils $date_formats = [ 'd-m-Y H:i' => 'd-m-Y H:i (e.g. '.$now->format('d-m-Y H:i').')', 'Y-m-d H:i' => 'Y-m-d H:i (e.g. '.$now->format('Y-m-d H:i').')', - 'm/d/Y h:i a' => 'm/d/Y h:i (e.g. '.$now->format('m/d/Y h:i a').')', + 'm/d/Y h:i a' => 'm/d/Y h:i a (e.g. '.$now->format('m/d/Y h:i a').')', 'H:i d-m-Y' => 'H:i d-m-Y (e.g. '.$now->format('H:i d-m-Y').')', 'h:i a m/d/Y' => 'h:i a m/d/Y (e.g. '.$now->format('h:i a m/d/Y').')', ]; @@ -416,17 +416,17 @@ abstract class Utils * * @param string $date a String expressed in the system.pages.dateformat.default format, with fallback to a * strtotime argument - * + * @param string $format a date format to use if possible * @return int the timestamp */ - public static function date2timestamp($date) + public static function date2timestamp($date, $format = null) { $config = self::getGrav()['config']; - $default_dateformat = $config->get('system.pages.dateformat.default'); + $dateformat = $format ?: $config->get('system.pages.dateformat.default'); // try to use DateTime and default format - if ($default_dateformat) { - $datetime = DateTime::createFromFormat($default_dateformat, $date); + if ($dateformat) { + $datetime = DateTime::createFromFormat($dateformat, $date); } else { $datetime = new DateTime($date); }