Hände tippen auf Laptop

[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>

Use Markdown to optimize websites for AI crawlers

The website is read and analyzed by AI crawlers in a fraction of a second. However, this process can be accelerated even further if TYPO3 returns Markdown instead of HTML to the AI ​​bot.

Go to news
Migration PHP

TYPO3: Finding unused files in fileadmin

Do you want to delete unused or orphaned files in fileadmin or another storage location? Unfortunately, there's no direct core functionality for this. But a small command in your site package can...

Go to news
Code

TYPO3: Editors with individual user_upload folders

Perhaps you're familiar with this client requirement? Editors should be able to add videos using the "Add media by URL" button. But the files shouldn't be located in fileadmin/user_upload/, but rather...

Go to news

TYPO3: Finding pages in mixed mode

In TYPO3, Mixed Mode refers to translated pages that contain content only partially related to the corresponding content in the main language. This is indicated in the backend by an error message. But...

Go to news
Code of the TYPO3 extension powermail

Extbase Extensions: Think extensibility with data, site and language

Today, I have a small request for the TYPO3 extension authors out there: Make sure your extensions are extensible. This will also promote the distribution of the corresponding plugins.

Go to news

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
Hände tippen auf Laptop