Skip to content

Instantly share code, notes, and snippets.

@smichaelsen
Last active December 21, 2015 21:09
Show Gist options
  • Save smichaelsen/6366433 to your computer and use it in GitHub Desktop.
Save smichaelsen/6366433 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<T3locallang>
<meta type="array">
<type>database</type>
<description>Language labels</description>
</meta>
<data type="array">
<languageKey index="default" type="array">
<label index="timespan.day">%d day</label>
<label index="timespan.days">%d days</label>
<label index="timespan.hour">%d hour</label>
<label index="timespan.hours">%d hours</label>
<label index="timespan.minute">%d minute</label>
<label index="timespan.minutes">%d minutes</label>
<label index="timespan.month">%d month</label>
<label index="timespan.months">%d months</label>
<label index="timespan.second">%d second</label>
<label index="timespan.seconds">%d seconds</label>
<label index="timespan.since">%s ago</label>
<label index="timespan.until">in %s</label>
<label index="timespan.year">%d year</label>
<label index="timespan.years">%d years</label>
</languageKey>
<languageKey index="de" type="array">
<label index="timespan.day">% Tag</label>
<label index="timespan.days">% Tagen</label>
<label index="timespan.hour">% Stunde</label>
<label index="timespan.hours">% Stunden</label>
<label index="timespan.month">% Monat</label>
<label index="timespan.months">% Monaten</label>
<label index="timespan.second">% Sekunde</label>
<label index="timespan.seconds">% Sekunden</label>
<label index="timespan.since">vor %</label>
<label index="timespan.until">in %</label>
<label index="timespan.year">% Jahr</label>
<label index="timespan.years">% Jahren</label>
</languageKey>
</data>
</T3locallang>
<?php
namespace Smichaelsen\MyExt\ViewHelpers;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
/**
* Class TimeSpanViewHelper
*
* Returns a human readable string stating the time interval between now and the given reference time.
* Uses the locallang.xml/xlf of $this->extensionName. The LLL keys are:
* timespan.day
* timespan.days
* timespan.hour
* timespan.hours
* timespan.minute
* timespan.minutes
* timespan.month
* timespan.months
* timespan.second
* timespan.seconds
* timespan.since
* timespan.until
* timespan.year
* timespan.years
*
* Usage: <m:timeSpan reference="{myTime}" />
* Example output: 4 days 12 hours 7 minutes and 42 seconds ago
* Example output: in 4 days 12 hours 7 minutes and 42 seconds
*
* TODO: make it possible to use a specific precision. e.g. precision="minutes" won't include the seconds in the output
*
* @package Smichaelsen\MyExt\ViewHelpers
*/
class TimeSpanViewHelper extends AbstractViewHelper{
/**
* @var string
*/
protected $extensionName = 'MyExt';
/**
* Initialize the arguments.
*
* @return void
* @api
*/
public function initializeArguments() {
parent::initializeArguments();
$this->registerArgument('reference', 'DateTime', 'The reference time', TRUE);
}
/**
* return string
*/
public function render() {
$messageParts = array();
$now = new \DateTime();
/** @var \DateTime $reference */
$reference = $this->arguments['reference'];
$difference = $now->diff($reference, TRUE);
$timeunits = array(
'year' => $difference->y,
'month' => $difference->m,
'day' => $difference->d,
'hour' => $difference->h,
'minute' => $difference->i,
'second' => $difference->s,
);
foreach ($timeunits as $label => $value) {
if ($value) {
$messageParts[] = LocalizationUtility::translate('timespan.' . $label . ($value > 1 ? 's' : ''), $this->extensionName, array($value));
}
}
return LocalizationUtility::translate('timespan.' . ($now > $reference ? 'since' : 'until'), $this->extensionName, array(join(' ', $messageParts)));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment