1b5b09966867d47f7d64150b1239bc4bb1388f6e
[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 class DateViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
77
78 /**
79 * @var boolean
80 */
81 protected $escapingInterceptorEnabled = FALSE;
82
83 /**
84 * Render the supplied DateTime object as a formatted date.
85 *
86 * @param mixed $date either a DateTime object or a string that is accepted by DateTime constructor
87 * @param string $format Format String which is taken to format the Date/Time
88 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
89 * @return string Formatted date
90 * @api
91 */
92 public function render($date = NULL, $format = 'Y-m-d') {
93 if ($date === NULL) {
94 $date = $this->renderChildren();
95 if ($date === NULL) {
96 return '';
97 }
98 }
99 if (!$date instanceof \DateTime) {
100 try {
101 if (is_integer($date)) {
102 $date = new \DateTime('@' . $date);
103 } else {
104 $date = new \DateTime($date);
105 }
106 $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
107 } catch (\Exception $exception) {
108 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('"' . $date . '" could not be parsed by DateTime constructor.', 1241722579);
109 }
110 }
111
112 if (strpos($format, '%') !== FALSE) {
113 return strftime($format, $date->format('U'));
114 } else {
115 return $date->format($format);
116 }
117
118 }
119 }
120
121 ?>