For coders TYPO3 Tech Corner

[PHP] Datum lesbar ausgeben in Fluid - z.B. "Vor 5 Minuten"

[PHP] Datum lesbar ausgeben in Fluid - z.B. "Vor 5 Minuten"

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>

Zurück

Kennst du das: Immer nur schnell schnell?

Wie wäre es einmal mit Zeit und Respekt für Codequalität? Arbeiten im Team? Automatisierte Tests?

Komm zu uns