How can I use 'xx time ago' for date format in Drupal 8 nodes ?
I want to replace "xx time ago" with {{ date }} in node.
In Drupal 7 it worked with this code :
<?php
print format_interval(time()-$node->created);
?>
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityHow can I use 'xx time ago' for date format in Drupal 8 nodes ?
I want to replace "xx time ago" with {{ date }} in node.
In Drupal 7 it worked with this code :
<?php
print format_interval(time()-$node->created);
?>
There are two ways to tackle this.
One is to just use the field formatters. If you have a DateTime field, just goto Manage Display, and change the Format to Time ago. Then click the gear, and pick some options. These are cache aware that will bubble up through the page, but you may end up with short cache lifetimes in some situations.
The other is to use a template_preprocess_node()
. This is preferable when you have a timestamp that isn't a field. Then you use the date formatter service to render out the timestamp in timeago format. It is preferable to use the formatDiff() versions rather than formatInterval() (while not formally deprecated, it is discouraged).
function MYTHEME_preprocess_node(&$variables) {
/** @var \Drupal\Core\Datetime\DateFormatterInterface $formatter */
$date_formatter = \Drupal::service('date.formatter');
$request_time = \Drupal::time()->getRequestTime();
$variables['foo'] = $date_formatter->formatDiff($request_time - 12345, $request_time, [
'granularity' => 3,
'return_as_object' => TRUE,
])->toRenderable();
}
When you specify 'return_as_object' => TRUE
with the formatDiff() functions, a FormattedDateDiff
object gets returned, which contains the formatted string as well as a mostly accurate [#cache]['max-age']
entry in a render array to make this work with Drupal 8's new caching system. Then you can just use {{ foo }}
in your Twig template; the render engine will do the right thing with the render array instead of a plain string.
Also read the docs on DateFormatterInterface
for the formatTimeDiffSince()
and formatTimeDiffUntil()
variants, and for the $options you can pass in
.
The easiest approach is to prepare it in ỳourtheme_preprocess_node() and then just print it in the twig template.
However, with D8's render caching, trying to do a time ago output on the server side can not work, unless you disable caching. It will be calculated on the first request and then never change again.
Instead, you want to send the time in an easy to parse markup, and then use JS to display it as a time ago format. See how d.o does that for comment dates: https://www.drupal.org/node/2704717 (it also includes functionality to switch between relative and absolute dates with a double click).
Date: Adds a filter for rendering the difference between dates.
and then the documentation at twig.sensiolabs.org/doc/extensions/date.html, thus {{ date|time_diff }}
– Danny Englander
Apr 26 '16 at 21:56
$this->date->formatTimeDiffSince($time_stamp);
or
\Drupal::service('date.formatter')->formatTimeDiffSince($time_stamp);
Twig has a native Date extension that provides a "Time Ago" filter
See http://twig.sensiolabs.org/doc/extensions/date.html
|time_diff is the one you want. The problem is Drupal doesn't include this extension by default.
So to get this in your twig templates do the following
Use the Filter in your Twig templates
{{ field_my_date|time_diff }}
Renders like "10 MONTHS AGO"
Here is Answer,
in your .theme file add below code function themename_preprocess_comment(&$variables) {
$date = $variables['comment']->getCreatedTime(); $variables['created'] = \Drupal::service('date.formatter')->formatInterval(REQUEST_TIME - $date); $variables['submitted'] = t('@author', array('@author' => $variables['author'], '!datetime' => '' . $variables['created'] . ' ago '));
}
and render in your comment.html.twig file below code
{{ created }} {{ 'ago'|t }}
hope it will helpfull thank you...