Using Drupal 8, I can change the date format, and adapt it to the country.

How can I do the same in a Drupal 7 site, where I do not want to create a node.tpl.php file in my theme since I need fast work?


You can use template_preprocess_node to alter submitted variable. Here is the sample code.

function awesome_preprocess_node(&$variables) {
  $node = $variables['node'];
  $date = format_date($node->created, 'custom', 'm-d-Y - H:i');
  $variables['submitted'] = t('Submitted by !username on !datetime', array('!username' => $variables['name'], '!datetime' => $date));

The most important line there is $date = format_date($node->created, 'custom', 'm-d-Y - H:i');. Change the format (m-d-Y - H:i) to any format you want.

See this page about PHP date format.

| improve this answer | |
  • thank you! but really I did not check that my task may be solved with admin pages. In Configuration - Date and time I can add new Date format and then change Standard date format choosing my new one. – tlito Aug 19 '16 at 7:16
  • Yeah that is possible as well. But your question is using template.php and that why I provided this answer. Anyway glad you solved your problem in a less complicated manner. – Gulok Aug 19 '16 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.