[BUGFIX] Date ViewHelper not using configured Timezones
[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 /**
14 * Formats a DateTime object.
15 *
16 * = Examples =
17 *
18 * <code title="Defaults">
19 * <f:format.date>{dateObject}</f:format.date>
20 * </code>
21 * <output>
22 * 1980-12-13
23 * (depending on the current date)
24 * </output>
25 *
26 * <code title="Custom date format">
27 * <f:format.date format="H:i">{dateObject}</f:format.date>
28 * </code>
29 * <output>
30 * 01:23
31 * (depending on the current time)
32 * </output>
33 *
34 * <code title="strtotime string">
35 * <f:format.date format="d.m.Y - H:i:s">+1 week 2 days 4 hours 2 seconds</f:format.date>
36 * </code>
37 * <output>
38 * 13.12.1980 - 21:03:42
39 * (depending on the current time, see http://www.php.net/manual/en/function.strtotime.php)
40 * </output>
41 *
42 * <code title="output date from unix timestamp">
43 * <f:format.date format="d.m.Y - H:i:s">@{someTimestamp}</f:format.date>
44 * </code>
45 * <output>
46 * 13.12.1980 - 21:03:42
47 * (depending on the current time. Don't forget the "@" in front of the timestamp see http://www.php.net/manual/en/function.strtotime.php)
48 * </output>
49 *
50 * <code title="Localized dates using strftime date format">
51 * <f:format.date format="%d. %B %Y">{dateObject}</f:format.date>
52 * </code>
53 * <output>
54 * 13. Dezember 1980
55 * (depending on the current date and defined locale. In the example you see the 1980-12-13 in a german locale)
56 * </output>
57 *
58 * <code title="Inline notation">
59 * {f:format.date(date: dateObject)}
60 * </code>
61 * <output>
62 * 1980-12-13
63 * (depending on the value of {dateObject})
64 * </output>
65 *
66 * <code title="Inline notation (2nd variant)">
67 * {dateObject -> f:format.date()}
68 * </code>
69 * <output>
70 * 1980-12-13
71 * (depending on the value of {dateObject})
72 * </output>
73 *
74 * @api
75 */
76
77 class DateViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
78
79 /**
80 * @var boolean
81 */
82 protected $escapingInterceptorEnabled = FALSE;
83
84 /**
85 * Render the supplied DateTime object as a formatted date.
86 *
87 * @param mixed $date either a DateTime object or a string that is accepted by DateTime constructor
88 * @param string $format Format String which is taken to format the Date/Time
89 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
90 * @return string Formatted date
91 * @api
92 */
93 public function render($date = NULL, $format = 'Y-m-d') {
94 if ($date === NULL) {
95 $date = $this->renderChildren();
96 if ($date === NULL) {
97 return '';
98 }
99 }
100 if (!$date instanceof \DateTime) {
101 try {
102 if (is_integer($date)) {
103 $date = new \DateTime('@' . $date);
104 } else {
105 $date = new \DateTime($date);
106 }
107 $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
108 } catch (\Exception $exception) {
109 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('"' . $date . '" could not be parsed by DateTime constructor.', 1241722579);
110 }
111 }
112
113 if (strpos($format, '%') !== FALSE) {
114 return strftime($format, $date->format('U'));
115 } else {
116 return $date->format($format);
117 }
118
119 }
120 }
121
122 ?>