How can I get comments creation date of a node and convert it to custom format in my theme, I tried to make some operations on node comments date in "MyTheme.theme" file and in MyTheme_preprocess_comment section.
- Is this about Drupal 6?Pierre.Vriens– Pierre.Vriens2016-07-09 19:48:43 +00:00Commented Jul 9, 2016 at 19:48
Add a comment |
1 Answer
/** * Implements hook_preprocess_comment */ function THEME_NAME_preprocess_comment(&$variables) { // Getting the node creation time stamp from the comment object. $date = $variables['comment']->getCreatedTime(); // Here you can use drupal's format_date() function, or some custom php date formatting. $variables['created'] = \Drupal::service('date.formatter')->formatInterval(REQUEST_TIME - $date); $variables['submitted'] = t('@username commented !datetime', array('@username' => $variables['author'], '!datetime' => '<span class="comments-ago">' . $variables['created'] . ' ago </span>')); } - Thank you very much,But, What's usage or profit
\Drupal::service('date.formatter')have in this Case?Mojtaba Reyhani– Mojtaba Reyhani2016-03-25 12:16:11 +00:00Commented Mar 25, 2016 at 12:16 - @MojtabaReyhani The 'date.formatter' service is the Drupal 8 way of formatting dates, and will take into consideration timezones, localization, etc.2016-03-25 13:29:40 +00:00Commented Mar 25, 2016 at 13:29
- @Vamsi formatInterval() is discouraged, but not officially deprecated yet. formatTimeDiffUntil(), formatTimeDiffSince, and formatDiff() are the preferred methods in the service for timeago foramts. There is an issue about this, but I don't have it handy.2016-03-25 13:33:59 +00:00Commented Mar 25, 2016 at 13:33