Skip to content

Instantly share code, notes, and snippets.

@potofcoffee
Last active August 29, 2015 13:59
Show Gist options
  • Save potofcoffee/10982075 to your computer and use it in GitHub Desktop.
Save potofcoffee/10982075 to your computer and use it in GitHub Desktop.
Generic strftime viewhelper for TYPO3 CMS 6.0 / Fluid
<?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));
}
}
@potofcoffee
Copy link
Author

How to use

  • Drop this file in the Classes/ViewHelpers folder of your extension and don't forget to correct the namespace declaration in line 3.
  • Declare a namespace at the top of your Fluid template:
{namespace ext=VENDOR\ExtensionName\ViewHelpers}
  • Use the viewhelper throughout your template like so:
<ext:strftime format="%d.%m.%Y">{myDateTimeObject}</ext:strftime>
<ext:strftime format="%d.%m.%Y">any kind of date/time string</ext:strftime>
<ext:strftime>now</ext:strftime>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment