mirror of
https://github.com/getgrav/grav.git
synced 2026-07-21 06:50:38 +02:00
Added support for custom page date format: https://github.com/getgrav/grav-plugin-admin/issues/135
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 = [];
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?php
|
||||
namespace Grav\Common;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
use RocketTheme\Toolbox\Event\Event;
|
||||
|
||||
/**
|
||||
@@ -449,6 +451,24 @@ abstract class Utils
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function date2timestamp($date)
|
||||
{
|
||||
$config = self::getGrav()['config'];
|
||||
$default_dateformat = $config->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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user