For example, if the distance is 47 minutes, it'll return
"about 1 hour". See the source for the complete wording list.
Integers are interpreted as seconds. So, by example to check the distance of time between
a created user and their last login:
{{ user.createdAt|distance_of_time_in_words(user.lastLoginAt) }} returns "less than a minute".
Set include_seconds to true if you want more detailed approximations if distance < 1 minute
Set include_months to true if you want approximations in months if days > 30
public distanceOfTimeInWordsFilter ( string | DateTime $from_time, string | DateTime $to_time = null, boolean $include_seconds = false, boolean $include_months = false ) : string | ||
$from_time | string | DateTime | |
$to_time | string | DateTime | |
$include_seconds | boolean | True to return distance in seconds when it's lower than a minute. |
$include_months | boolean | |
return | string |
/** * Launching tests twice: dates as strings and as DateTime objects. * Both arguments are accepted. * * @param $fromTime * @param $toTime * @param $expectedTranslation * @param bool $includeSeconds * @param bool $includeMonths */ private function assertDistanceOfTimeExpectation($fromTime, $toTime, $expectedTranslation, $includeSeconds = self::DEFAULT_INCLUDE_SECONDS, $includeMonths = self::DEFAULT_INCLUDE_MONTHS) { $fromDateTime = \DateTime::createFromFormat(self::DATE_FORMAT, $fromTime, new \DateTimeZone('GMT')); $toDateTime = \DateTime::createFromFormat(self::DATE_FORMAT, $toTime, new \DateTimeZone('GMT')); $translationFromDateStrings = $this->extension->distanceOfTimeInWordsFilter($fromTime, $toTime, $includeSeconds, $includeMonths); $translationFromDateTimes = $this->extension->distanceOfTimeInWordsFilter($fromDateTime, $toDateTime, $includeSeconds, $includeMonths); $this->assertEquals($expectedTranslation, $translationFromDateStrings); $this->assertEquals($expectedTranslation, $translationFromDateTimes); }