For coders TYPO3 Tech Corner

[PHP] Date readable output in Fluid - e.g. "5 minutes ago"

[PHP] Date readable output in Fluid - e.g. "5 minutes ago"

Maybe you don't always want to output a date of a news absolutely, but sometimes also relative. When it comes to usability and liveliness, a "3 months ago" looks nicer than an absolute date. Your own ViewHelper can help you with this.

ReadableDateViewHelper.php:

<?php declare(strict_types=1); namespace In2code\Lux\ViewHelpers\Format; use TYPO3\CMS\Extbase\Utility\LocalizationUtility; use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; /** * Class ReadableDateViewHelper * * Example call in FLUID with <lux:format.readableDate>{date}</lux:format.readableDate> */ class ReadableDateViewHelper extends AbstractViewHelper { /** * @return void */ public function initializeArguments() { parent::initializeArguments(); $this->registerArgument('date', \DateTime::class, 'Datetime', false); } /** * @return string */ public function render(): string { $date = $this->getDate(); $deltaTimestamp = time() - $date->getTimestamp(); $delta = $date->diff(new \DateTime()); if ($deltaTimestamp < 3600) { return $this->renderMinutes($delta); } elseif ($deltaTimestamp < 86400) { return $this->renderHours($delta); } elseif ($deltaTimestamp < 604800) { return $this->renderDays($delta); } else { return $this->renderDate($date); } } /** * @param \DateInterval $date * @return string */ protected function renderMinutes(\DateInterval $date): string { $minutes = $date->i; return (string)LocalizationUtility::translate( 'LLL:EXT:lux/Resources/Private/Language/locallang_db.xlf:readabledate.minutes', 'Lux', [$minutes] ); } /** * @param \DateInterval $date * @return string */ protected function renderHours(\DateInterval $date): string { $hours = $date->h; return (string)LocalizationUtility::translate( 'LLL:EXT:lux/Resources/Private/Language/locallang_db.xlf:readabledate.hours', 'Lux', [$hours] ); } /** * @param \DateInterval $date * @return string */ protected function renderDays(\DateInterval $date): string { $days = $date->d; return (string)LocalizationUtility::translate( 'LLL:EXT:lux/Resources/Private/Language/locallang_db.xlf:readabledate.days', 'Lux', [$days] ); } /** * @param \DateTime $date * @return string */ protected function renderDate(\DateTime $date): string { $format = (string)LocalizationUtility::translate( 'LLL:EXT:lux/Resources/Private/Language/locallang_db.xlf:readabledate.date' ); return $date->format($format); } /** * @return \DateTime */ protected function getDate(): \DateTime { $pathAndFilename = $this->renderChildren(); if (!empty($this->arguments['date'])) { $pathAndFilename = $this->arguments['date']; } return $pathAndFilename; } }

de.locallang_db.xlf:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?> <xliff version="1.0"> <file source-language="en" target-language="de" datatype="plaintext" original="messages" date="2018-02-06T12:00:00Z" product-name="lux"> <header/> <body> <trans-unit id="readabledate.minutes"> <source>%s minutes ago</source> <target state="translated">vor %s Minuten</target> </trans-unit> <trans-unit id="readabledate.hours"> <source>%s hours ago</source> <target state="translated">vor %s Stunden</target> </trans-unit> <trans-unit id="readabledate.days"> <source>%s days ago</source> <target state="translated">vor %s Tagen</target> </trans-unit> <trans-unit id="readabledate.date"> <source>Y-m-d</source> <target state="translated">d.m.Y</target> </trans-unit> </body> </file> </xliff>

"Code faster, look at the time" - does this sound familiar to you?

How about time and respect for code quality? Working in a team? Automated tests?

Join us

SQL: Show all tables sorted by size in descending order

Lately I've been using the SQL command more often to find out which tables in the TYPO3 database are the largest. I've published the snippet once.

Go to news

TYPO3 12 with CKEditor 5: Styles in a single selection

If you set a link in the RTE in TYPO3, you may have to choose between different link classes, for example to create buttons in the frontend. What's new in TYPO3 12 is that you can select not just one...

Go to news

Null-Safe Operator in the TYPO3 area

With the introduction of PHP8, problems with undefined arrays or variables in general can arise in many places. Here are a few examples and simple solutions.

Go to news

Delete the first/last lines of a (SQL) file

There isn't much to say about the following commands. Sometimes it can be useful to delete the first (or last) X lines from a file. And if the file is too large to open with a conventional program, a...

Go to news

b13/container: Add and modify child elements in edit view

Unlike gridelements, you cannot manage the child elements in the B13 Container extension when you open the container in the editing view. I would be happy to show you how you can quickly install this...

Go to news

Menu comparison: Numbers, numbers, numbers

Go to news