‪TYPO3CMS  9.5
DateViewHelper.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
24 
101 class ‪DateViewHelper extends AbstractViewHelper
102 {
103  use CompileWithContentArgumentAndRenderStatic;
104 
110  protected ‪$escapeChildren = false;
111 
115  public function ‪initializeArguments()
116  {
117  $this->registerArgument('date', 'mixed', 'Either an object implementing DateTimeInterface or a string that is accepted by DateTime constructor');
118  $this->registerArgument('format', 'string', 'Format String which is taken to format the Date/Time', false, '');
119  $this->registerArgument('base', 'mixed', 'A base time (an object implementing DateTimeInterface or a string) used if $date is a relative date specification. Defaults to current time.');
120  }
121 
130  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
131  {
132  $format = $arguments['format'];
133  $base = $arguments['base'] ?? GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp');
134  if (is_string($base)) {
135  $base = trim($base);
136  }
137 
138  if ($format === '') {
139  $format = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d';
140  }
141 
142  $date = $renderChildrenClosure();
143  if ($date === null) {
144  return '';
145  }
146 
147  if (is_string($date)) {
148  $date = trim($date);
149  }
150 
151  if ($date === '') {
152  $date = 'now';
153  }
154 
155  if (!$date instanceof \DateTimeInterface) {
156  try {
157  $base = $base instanceof \DateTimeInterface ? $base->format('U') : strtotime((‪MathUtility::canBeInterpretedAsInteger($base) ? '@' : '') . $base);
158  $dateTimestamp = strtotime((‪MathUtility::canBeInterpretedAsInteger($date) ? '@' : '') . $date, $base);
159  $date = new \DateTime('@' . $dateTimestamp);
160  $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
161  } catch (\Exception $exception) {
162  throw new Exception('"' . $date . '" could not be parsed by \DateTime constructor: ' . $exception->getMessage(), 1241722579);
163  }
164  }
165 
166  if (strpos($format, '%') !== false) {
167  return strftime($format, $date->format('U'));
168  }
169  return $date->format($format);
170  }
171 }
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:73
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper\initializeArguments
‪initializeArguments()
Definition: DateViewHelper.php:113
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:49
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper
Definition: DateViewHelper.php:102
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: DateViewHelper.php:108
‪TYPO3\CMS\Fluid\ViewHelpers\Format
Definition: AbstractEncodingViewHelper.php:2
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: DateViewHelper.php:128