89a545eef619221cdf7d8e1ffa17bcbc32611e2d
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / DateViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
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. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13 use TYPO3\CMS\Core\Utility\MathUtility;
14 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
15 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
16 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
17 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
18
19 /**
20 * Formats a \DateTime object.
21 *
22 * = Examples =
23 *
24 * <code title="Defaults">
25 * <f:format.date>{dateObject}</f:format.date>
26 * </code>
27 * <output>
28 * 1980-12-13
29 * (depending on the current date)
30 * </output>
31 *
32 * <code title="Custom date format">
33 * <f:format.date format="H:i">{dateObject}</f:format.date>
34 * </code>
35 * <output>
36 * 01:23
37 * (depending on the current time)
38 * </output>
39 *
40 * <code title="strtotime string">
41 * <f:format.date format="d.m.Y - H:i:s">+1 week 2 days 4 hours 2 seconds</f:format.date>
42 * </code>
43 * <output>
44 * 13.12.1980 - 21:03:42
45 * (depending on the current time, see http://www.php.net/manual/en/function.strtotime.php)
46 * </output>
47 *
48 * <code title="Localized dates using strftime date format">
49 * <f:format.date format="%d. %B %Y">{dateObject}</f:format.date>
50 * </code>
51 * <output>
52 * 13. Dezember 1980
53 * (depending on the current date and defined locale. In the example you see the 1980-12-13 in a german locale)
54 * </output>
55 *
56 * <code title="Inline notation">
57 * {f:format.date(date: dateObject)}
58 * </code>
59 * <output>
60 * 1980-12-13
61 * (depending on the value of {dateObject})
62 * </output>
63 *
64 * <code title="Inline notation (2nd variant)">
65 * {dateObject -> f:format.date()}
66 * </code>
67 * <output>
68 * 1980-12-13
69 * (depending on the value of {dateObject})
70 * </output>
71 *
72 * @api
73 */
74 class DateViewHelper extends AbstractViewHelper implements CompilableInterface {
75
76 /**
77 * @var bool
78 */
79 protected $escapingInterceptorEnabled = FALSE;
80
81 /**
82 * Render the supplied DateTime object as a formatted date.
83 *
84 * @param mixed $date either a DateTime object or a string that is accepted by DateTime constructor
85 * @param string $format Format String which is taken to format the Date/Time
86 *
87 * @return string Formatted date
88 * @throws Exception
89 * @api
90 */
91 public function render($date = NULL, $format = '') {
92 return self::renderStatic(
93 array(
94 'date' => $date,
95 'format' => $format
96 ),
97 $this->buildRenderChildrenClosure(),
98 $this->renderingContext
99 );
100 }
101
102 /**
103 * @param array $arguments
104 * @param callable $renderChildrenClosure
105 * @param RenderingContextInterface $renderingContext
106 *
107 * @return string
108 * @throws Exception
109 */
110 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
111 $date = $arguments['date'];
112 $format = $arguments['format'];
113 if ($format === '') {
114 $format = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d';
115 }
116
117 if ($date === NULL) {
118 $date = $renderChildrenClosure();
119 if ($date === NULL) {
120 return '';
121 }
122 }
123 if (!$date instanceof \DateTime) {
124 try {
125 if (MathUtility::canBeInterpretedAsInteger($date)) {
126 $date = new \DateTime('@' . $date);
127 } else {
128 $date = new \DateTime($date);
129 }
130 $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
131 } catch (\Exception $exception) {
132 throw new Exception('"' . $date . '" could not be parsed by \DateTime constructor.', 1241722579);
133 }
134 }
135
136 if (strpos($format, '%') !== FALSE) {
137 return strftime($format, $date->format('U'));
138 } else {
139 return $date->format($format);
140 }
141 }
142 }