‪TYPO3CMS  10.4
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 = 'now';
154  }
155 
156  if (!$date instanceof \DateTimeInterface) {
157  try {
158  $base = $base instanceof \DateTimeInterface ? (int)$base->format('U') : (int)strtotime((‪MathUtility::canBeInterpretedAsInteger($base) ? '@' : '') . $base);
159  $dateTimestamp = strtotime((‪MathUtility::canBeInterpretedAsInteger($date) ? '@' : '') . $date, $base);
160  $date = new \DateTime('@' . $dateTimestamp);
161  $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
162  } catch (\Exception $exception) {
163  throw new Exception('"' . $date . '" could not be parsed by \DateTime constructor: ' . $exception->getMessage(), 1241722579);
164  }
165  }
166 
167  if (strpos($format, '%') !== false) {
168  return strftime($format, (int)$date->format('U'));
169  }
170  return $date->format($format);
171  }
172 }
‪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:5
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: DateViewHelper.php:129