6eaa2b269b976be0c6a86594080d63b5e636bcad
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / DateViewHelper.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 package "Fluid". *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 /**
24 * Formats a DateTime object.
25 *
26 * = Examples =
27 *
28 * <code title="Defaults">
29 * <f:format.date>{dateObject}</f:format.date>
30 * </code>
31 * <output>
32 * 1980-12-13
33 * (depending on the current date)
34 * </output>
35 *
36 * <code title="Custom date format">
37 * <f:format.date format="H:i">{dateObject}</f:format.date>
38 * </code>
39 * <output>
40 * 01:23
41 * (depending on the current time)
42 * </output>
43 *
44 * <code title="strtotime string">
45 * <f:format.date format="d.m.Y - H:i:s">+1 week 2 days 4 hours 2 seconds</f:format.date>
46 * </code>
47 * <output>
48 * 13.12.1980 - 21:03:42
49 * (depending on the current time, see http://www.php.net/manual/en/function.strtotime.php)
50 * </output>
51 *
52 * <code title="output date from unix timestamp">
53 * <f:format.date format="d.m.Y - H:i:s">@{someTimestamp}</f:format.date>
54 * </code>
55 * <output>
56 * 13.12.1980 - 21:03:42
57 * (depending on the current time. Don't forget the "@" in front of the timestamp see http://www.php.net/manual/en/function.strtotime.php)
58 * </output>
59 *
60 * <code title="Inline notation">
61 * {f:format.date(date: dateObject)}
62 * </code>
63 * <output>
64 * 1980-12-13
65 * (depending on the value of {dateObject})
66 * </output>
67 *
68 * <code title="Inline notation (2nd variant)">
69 * {dateObject -> f:format.date()}
70 * </code>
71 * <output>
72 * 1980-12-13
73 * (depending on the value of {dateObject})
74 * </output>
75 *
76 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
77 * @api
78 */
79 class Tx_Fluid_ViewHelpers_Format_DateViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
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 * @return string Formatted date
87 * @author Christopher Hlubek <hlubek@networkteam.com>
88 * @author Bastian Waidelich <bastian@typo3.org>
89 * @api
90 */
91 public function render($date = NULL, $format = 'Y-m-d') {
92 if ($date === NULL) {
93 $date = $this->renderChildren();
94 if ($date === NULL) {
95 return '';
96 }
97 }
98 if (!$date instanceof DateTime) {
99 try {
100 $date = new DateTime($date);
101 } catch (Exception $exception) {
102 throw new Tx_Fluid_Core_ViewHelper_Exception('"' . $date . '" could not be parsed by DateTime constructor.', 1241722579);
103 }
104 }
105 return $date->format($format);
106 }
107 }
108 ?>