‪TYPO3CMS  11.5
DateViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
25 
102 class ‪DateViewHelper extends AbstractViewHelper
103 {
104  use CompileWithContentArgumentAndRenderStatic;
105 
111  protected ‪$escapeChildren = false;
112 
116  public function ‪initializeArguments()
117  {
118  $this->registerArgument('date', 'mixed', 'Either an object implementing DateTimeInterface or a string that is accepted by DateTime constructor');
119  $this->registerArgument('format', 'string', 'Format String which is taken to format the Date/Time', false, '');
120  $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.');
121  }
122 
131  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
132  {
133  $format = $arguments['format'] ?? '';
134  $base = $arguments['base'] ?? GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp');
135  if (is_string($base)) {
136  $base = trim($base);
137  }
138 
139  if ($format === '') {
140  $format = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d';
141  }
142 
143  $date = $renderChildrenClosure();
144  if ($date === null) {
145  return '';
146  }
147 
148  if (is_string($date)) {
149  $date = trim($date);
150  }
151 
152  if ($date === '') {
153  $date = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp', 'now');
154  }
155 
156  if (!$date instanceof \DateTimeInterface) {
157  $base = $base instanceof \DateTimeInterface
158  ? (int)$base->format('U')
159  : (int)strtotime((‪MathUtility::canBeInterpretedAsInteger($base) ? '@' : '') . $base);
160  $dateTimestamp = strtotime((‪MathUtility::canBeInterpretedAsInteger($date) ? '@' : '') . $date, $base);
161  if ($dateTimestamp === false) {
162  throw new Exception('"' . $date . '" could not be converted to a timestamp. Probably due to a parsing error.', 1241722579);
163  }
164  $date = (new \DateTime())->setTimestamp($dateTimestamp);
165  }
166 
167  if (str_contains($format, '%')) {
168  // @todo Replace deprecated strftime in php 8.1. Suppress warning in v11.
169  return @strftime($format, (int)$date->format('U'));
170  }
171  return $date->format($format);
172  }
173 }
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper\initializeArguments
‪initializeArguments()
Definition: DateViewHelper.php:114
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper
Definition: DateViewHelper.php:103
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: DateViewHelper.php:109
‪TYPO3\CMS\Fluid\ViewHelpers\Format
Definition: AbstractEncodingViewHelper.php:16
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: DateViewHelper.php:129