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