Add support for custom page-level dateformat field (core only)

This commit is contained in:
Andy Miller
2016-02-12 15:33:23 -07:00
parent 3a46dc3dcd
commit 7a1d9e454b
2 changed files with 30 additions and 9 deletions

View File

@@ -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.
*

View File

@@ -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);
}