0

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.

1
  • Is this about Drupal 6? Commented Jul 9, 2016 at 19:48

1 Answer 1

4
/** * 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>')); } 
3
  • Thank you very much,But, What's usage or profit \Drupal::service('date.formatter') have in this Case? Commented 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. Commented 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. Commented Mar 25, 2016 at 13:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.