I use Drupal 8 and want the date of comments just show "xx time ago".
I added this code to mytheme.theme
, but it does not work:
function mytheme_preprocess_comment(&$variables) {
/** @var \Drupal\Core\Datetime\DateFormatterInterface $formatter */
$date_formatter = \Drupal::service('date.formatter');
$variables['foo'] = $date_formatter->formatDiff(REQUEST_TIME - 12345, REQUEST_TIME, [
'granularity' => 3,
'return_as_object' => TRUE, ]); }
And I use {{ foo }}
but that does not show "time ago" either. Other things I tried (and which didn't work either):
{{ created|date_modify }}
{{ created|time_deff }}
{{ created }}
Any help/hints about how I can do this?
Note: using the same logic as in my previous question (How can I use 'xx time ago' as date format?) and using MYTHEME_preprocess_comment
instead (as suggested by Neograph734) does not seem to work for comments.