TYPO3 6.2 – localized date output in fluidtemplates based on strftime

Rendering a DateTime Object in a fluid template based on localized PHP settings seems not too easy – for me, the easiest way to solve this problem is via custom viewhelper, which renders a given DateTime based on the PHP “strftime” function.

First, we need to implement our ViewHelper.
Diretory: PATH_TO_YOUR_EXTENSION/Classes/ViewHelpers

Create a new file called “StrftimeViewHelper.php”:

renderChildren();
            if ($date === NULL) {
                return '';
            }
        }

        if ($date instanceof \DateTime) {
            try {

                return strftime($format, $date->getTimestamp());
            } catch (Exception $exception) {
                throw new \TYPO3\CMS\Fluid\ViewHelpers\Exception('"' . $date . '" was DateTime and could not be converted to UNIX-Timestamp by DateTime.', 200000001);
            }
        }
        return strftime($format, (int)$date);
    }
}
?>

Please be sure to use the correct Vendor name (MY_VENDOR_NAME) and extension key (MY_EXTENSION_KEY) in the namespace declaration.

Now we have to add the namespace to our view (e.g. “List.html”) so we can use our added custom ViewHelper:

{namespace tx=MY_VENDOR_NAME\MY_EXTENSION_KEY\ViewHelpers}

Using the view helper width DateTime Objects is quite easy now:


 

 
{MyDateTime}
 

 
1297897200


This article is based on this forum entry http://www.typo3.net/forum/thematik/zeige/thema/103967/?show=1 – but the solutions showed there refers to an older version of TYPO3 without using namespaces.

One thought on “TYPO3 6.2 – localized date output in fluidtemplates based on strftime

  1. Christian Platt

    If you use in Template under 6.2(18) you will get

    Uncaught TYPO3 Exception
    #1289386765: Could not analyse class:

    Just leave the “format” and it works
    did it for me!

Leave a Reply

Your email address will not be published. Required fields are marked *