Skip to content

Instantly share code, notes, and snippets.

@einpraegsam
Created July 24, 2018 14:28
Show Gist options
  • Save einpraegsam/bad3062b8a56e8a3873b828dc82b6463 to your computer and use it in GitHub Desktop.
Save einpraegsam/bad3062b8a56e8a3873b828dc82b6463 to your computer and use it in GitHub Desktop.
ReadableDateViewHelper to show a date in TYPO3 Fluid with a readable date like: "2 Minutes ago", "4 hours ago", "2 days ago" or a date. Tipp: use a normal date viewhelper date in a title-attribute in a wrapping (e.g.) span tag to have a exact date and time while hovering the readable date.
<?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>
<?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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment