Andrew Fletcher published: 26 July 2021 2 minutes read
You can create a new date/time format by navigating to:
Admin -> Configuration -> Regional and Language -> Date and Time format and click on 'Add Format'.
Once you’ve created date format, you can control the way date is displayed on the node using preprocess function.
In {theme_name}.theme file, add the following lines
/** * Implements hook_preprocess_node */ function THEME_NAME_preprocess_node(&$variables) { // Getting the node creation time stamp from the node object. $date = $variables['node']->getCreatedTime(); // Here you can use drupal's format_date() function, or some custom PHP date formatting. $variables['date'] = \Drupal::service('date.formatter')->format($date, 'date_text'); }
Note - date_text is the name of date format created.
Whereas, to create the date format for time ago?
You can change the date format for comment using template_preprocess_comment(). For example, normally in comments, the comment created time is displayed like Mon, 26/07/2021 - 16:05. This can be changed so the format looks like 2 weeks 3 days ago.
/** * 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>') ); }
The Drupal::service function can also be used to apply the format to a non node date field. Such as creating a custom module /** * Format the amount of time since the last migration for an area * * @param [String] $changed * * @return String */ public function generate_formatted_date($changed) { $date = ''; if ($changed != '') { $date = \Drupal::service('date.formatter')->formatInterval(REQUEST_TIME - $changed); } return $date; }
Related articles
Andrew Fletcher
•
04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher
•
26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher
•
17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...