2 namespace TYPO3\CMS\Fluid\ViewHelpers\Format
;
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
11 * The TYPO3 project - inspiring people to share! *
14 use TYPO3\CMS\Core\Utility\MathUtility
;
15 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface
;
16 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
;
17 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception
;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface
;
21 * Formats a \DateTime object.
25 * <code title="Defaults">
26 * <f:format.date>{dateObject}</f:format.date>
30 * (depending on the current date)
33 * <code title="Custom date format">
34 * <f:format.date format="H:i">{dateObject}</f:format.date>
38 * (depending on the current time)
41 * <code title="Relative date with given time">
42 * <f:format.date format="Y" base="{dateObject}">-1 year</f:format.date>
46 * (assuming dateObject is in 2017)
49 * <code title="strtotime string">
50 * <f:format.date format="d.m.Y - H:i:s">+1 week 2 days 4 hours 2 seconds</f:format.date>
53 * 13.12.1980 - 21:03:42
54 * (depending on the current time, see http://www.php.net/manual/en/function.strtotime.php)
57 * <code title="Localized dates using strftime date format">
58 * <f:format.date format="%d. %B %Y">{dateObject}</f:format.date>
62 * (depending on the current date and defined locale. In the example you see the 1980-12-13 in a german locale)
65 * <code title="Inline notation">
66 * {f:format.date(date: dateObject)}
70 * (depending on the value of {dateObject})
73 * <code title="Inline notation (2nd variant)">
74 * {dateObject -> f:format.date()}
78 * (depending on the value of {dateObject})
83 class DateViewHelper
extends AbstractViewHelper
implements CompilableInterface
{
88 protected $escapingInterceptorEnabled = FALSE;
91 * Render the supplied DateTime object as a formatted date.
93 * @param mixed $date either a DateTime object or a string that is accepted by DateTime constructor
94 * @param string $format Format String which is taken to format the Date/Time
95 * @param mixed $base A base time (a DateTime object or a string) used if $date is a relative date specification. Defaults to current time.
97 * @return string Formatted date
101 public function render($date = NULL, $format = '', $base = NULL) {
102 return static::renderStatic(
108 $this->buildRenderChildrenClosure(),
109 $this->renderingContext
114 * @param array $arguments
115 * @param \Closure $renderChildrenClosure
116 * @param RenderingContextInterface $renderingContext
121 static public function renderStatic(array $arguments, \Closure
$renderChildrenClosure, RenderingContextInterface
$renderingContext) {
122 $date = $arguments['date'];
123 $format = $arguments['format'];
124 $base = $arguments['base'] === NULL ?
time() : $arguments['base'];
125 if ($format === '') {
126 $format = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?
: 'Y-m-d';
129 if ($date === NULL) {
130 $date = $renderChildrenClosure();
131 if ($date === NULL) {
135 if (!$date instanceof \DateTime
) {
137 $base = $base instanceof \DateTime ?
$base->format('U') : strtotime((MathUtility
::canBeInterpretedAsInteger($base) ?
'@' : '') . $base);
138 $dateTimestamp = strtotime((MathUtility
::canBeInterpretedAsInteger($date) ?
'@' : '') . $date, $base);
139 $date = new \
DateTime('@' . $dateTimestamp);
140 $date->setTimezone(new \
DateTimeZone(date_default_timezone_get()));
141 } catch (\Exception
$exception) {
142 throw new Exception('"' . $date . '" could not be parsed by \DateTime constructor.', 1241722579);
146 if (strpos($format, '%') !== FALSE) {
147 return strftime($format, $date->format('U'));
149 return $date->format($format);