TYPO3 API  SVNRelease
DateViewHelper.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script belongs to the FLOW3 package "Fluid".                      *
00005  *                                                                        *
00006  * It is free software; you can redistribute it and/or modify it under    *
00007  * the terms of the GNU Lesser General Public License as published by the *
00008  * Free Software Foundation, either version 3 of the License, or (at your *
00009  * option) any later version.                                             *
00010  *                                                                        *
00011  * This script is distributed in the hope that it will be useful, but     *
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00013  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00014  * General Public License for more details.                               *
00015  *                                                                        *
00016  * You should have received a copy of the GNU Lesser General Public       *
00017  * License along with the script.                                         *
00018  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00019  *                                                                        *
00020  * The TYPO3 project - inspiring people to share!                         *
00021  *                                                                        */
00022 
00023 /**
00024  * Formats a DateTime object.
00025  *
00026  * = Examples =
00027  *
00028  * <code title="Defaults">
00029  * <f:format.date>{dateObject}</f:format.date>
00030  * </code>
00031  * <output>
00032  * 1980-12-13
00033  * (depending on the current date)
00034  * </output>
00035  *
00036  * <code title="Custom date format">
00037  * <f:format.date format="H:i">{dateObject}</f:format.date>
00038  * </code>
00039  * <output>
00040  * 01:23
00041  * (depending on the current time)
00042  * </output>
00043  *
00044  * <code title="Localized dates using strftime date format">
00045  * <f:format.date format="%d. %B %Y">{dateObject}</f:format.date>
00046  * </code>
00047  * <output>
00048  * 13. Dezember 1980
00049  * (depending on the current date and defined locale. In the example you see the 1980-12-13 in a german locale)
00050  * </output>
00051  *
00052  * <code title="strtotime string">
00053  * <f:format.date format="d.m.Y - H:i:s">+1 week 2 days 4 hours 2 seconds</f:format.date>
00054  * </code>
00055  * <output>
00056  * 13.12.1980 - 21:03:42
00057  * (depending on the current time, see http://www.php.net/manual/en/function.strtotime.php)
00058  * </output>
00059  *
00060  * <code title="output date from unix timestamp">
00061  * <f:format.date format="d.m.Y - H:i:s">@{someTimestamp}</f:format.date>
00062  * </code>
00063  * <output>
00064  * 13.12.1980 - 21:03:42
00065  * (depending on the current time. Don't forget the "@" in front of the timestamp see http://www.php.net/manual/en/function.strtotime.php)
00066  * </output>
00067  *
00068  * <code title="Inline notation">
00069  * {f:format.date(date: dateObject)}
00070  * </code>
00071  * <output>
00072  * 1980-12-13
00073  * (depending on the value of {dateObject})
00074  * </output>
00075  *
00076  * <code title="Inline notation (2nd variant)">
00077  * {dateObject -> f:format.date()}
00078  * </code>
00079  * <output>
00080  * 1980-12-13
00081  * (depending on the value of {dateObject})
00082  * </output>
00083  *
00084  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00085  * @api
00086  */
00087 class Tx_Fluid_ViewHelpers_Format_DateViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
00088 
00089     /**
00090      * Render the supplied DateTime object as a formatted date.
00091      *
00092      * @param mixed $date either a DateTime object or a string that is accepted by DateTime constructor
00093      * @param string $format Format String which is taken to format the Date/Time
00094      * @return string Formatted date
00095      * @author Christopher Hlubek <hlubek@networkteam.com>
00096      * @author Bastian Waidelich <bastian@typo3.org>
00097      * @api
00098      */
00099     public function render($date = NULL, $format = 'Y-m-d') {
00100         if ($date === NULL) {
00101             $date = $this->renderChildren();
00102             if ($date === NULL) {
00103                 return '';
00104             }
00105         }
00106         if (!$date instanceof DateTime) {
00107             try {
00108                 $date = new DateTime($date);
00109             } catch (Exception $exception) {
00110                 throw new Tx_Fluid_Core_ViewHelper_Exception('"' . $date . '" could not be parsed by DateTime constructor.', 1241722579);
00111             }
00112         }
00113         return $date->format($format);
00114     }
00115 }
00116 ?>