Last active
August 29, 2015 13:59
-
-
Save potofcoffee/10982075 to your computer and use it in GitHub Desktop.
Generic strftime viewhelper for TYPO3 CMS 6.0 / Fluid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace VENDOR\ExtensionName\ViewHelpers; | |
/** | |
* Formats a Timestamp or DateTime-Object in strftime() | |
* @api | |
*/ | |
class StrftimeViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper { | |
/** | |
* Render the supplied DateTime object as a formatted date using strftime. | |
* | |
* @param mixed $date either a DateTime object or a string (UNIX-Timestamp) | |
* @param string $format Format String which is taken to format the Date/Time | |
* @return string Formatted date | |
* @api | |
*/ | |
public function render($date = NULL, $format = '%A, %d. %B %Y') { | |
if ($date === NULL) { | |
$date = $this->renderChildren(); | |
if ($date === NULL) { | |
return ''; | |
} | |
} | |
// Note: Datetime needs to be in global namespace, so it's \DateTime | |
if ($date instanceof \DateTime) { | |
try { | |
return strftime($format, $date->getTimestamp()); | |
} catch (Exception $exception) { | |
throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('"' . $date . '" was DateTime and could not be converted to UNIX-Timestamp by DateTime.', 200000001); | |
} | |
} | |
return strftime($format, (int)strtotime($date)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use