Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By ItangSanjana on
Hello, Drupal novice here, I need custom date format for node & comment. Below is what I put in my template.php file. Everything seems to work but is this correct or safe? Please let me know. TIA
<?php
function mytheme_preprocess_node(&$variables) {
$node = $variables['node'];
$variables['date'] = format_date($node->created, 'custom', 'l, d/m/Y');
if (variable_get('node_submitted_' . $node->type, TRUE)) {
$variables['display_submitted'] = TRUE;
$variables['submitted'] = t('Submitted by !username on !datetime', array('!username' => $variables['name'], '!datetime' => $variables['date']));
$variables['user_picture'] = theme_get_setting('toggle_node_user_picture') ? theme('user_picture', array('account' => $node)) : '';
}
else {
$variables['display_submitted'] = FALSE;
$variables['submitted'] = '';
$variables['user_picture'] = '';
}
}
function mytheme_preprocess_comment(&$variables) {
$comment = $variables['elements']['#comment'];
$node = $variables['elements']['#node'];
$variables['created'] = format_date($comment->created, 'custom', 'l, d/m/Y');
$variables['changed'] = format_date($comment->changed, 'custom', 'l, d/m/Y');
$variables['submitted'] = t('Submitted by !username on !datetime', array('!username' => $variables['author'], '!datetime' => $variables['created']));
}
?>
Comments
Yes, it's correct, however...
I tried this and it worked on my Drupal 7 installation. However, I've been wondering if it's possible to add code that alters this type of thing in the node.tpl.php file itself, as in the way I used to do this in Drupal 6:
Does anyone know if something like this is possible? This code no longer works. Has it been deprecated, and if so, is there another 'best practices' approach?
...
You can do something like this:
Proprocessing is best practice, but really there is nothing wrong with doing this, we're already accepting function calls in templates in D7 (render(), t(), etc), can't see why format_date() would be so evil.
The ! is a placeholder that will print text "as is" for text already sanitized, you can read more about t() here: http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/t/7
$terms
variable no longer exists in Drupal, its all done with fields, there are some posts floating around that show you how to recreate this variable for D7 if you really want to do this. Search or look through the forum, some were posted just a few days ago.Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
@nateeanes & @Jeff Burnz,
@nateeanes & @Jeff Burnz, thanks for the info, very useful ..
I need to change my date
I need to change my date format, but I'm not php-literate to understand everything that's going on here. I understand the tpl.php examples, but I'd like to stick to best practices and not put it there. Could someone give me an example of simply reformating $date in template.php if called by a node?
Go to:
Go to: /admin/config/regional/date-time/formats/add
Create desired time format, i.e. l, d/m/Y or see http://id.php.net/manual/en/function.date.php for available options.
Add format.
Go to: /admin/config/regional/date-time
Set medium data type into the desired one.
HTH
Thanks for this smart solution
It may be obvious to experienced users... but I couldn't come up with this.
Thank you!
Thanks for the solution... but
Thanks for the solution, but where does the medium format get assigned to the post date format? This works, but it seems like a hack to me.
Steve
Thank You Drupal Community for all the help you continue to give me, especially when I need it most!
No, not hack :) medium format
No, not hack :) medium format is Drupal default for $variables['date'] in node. CMIIW
Theming the date format in D7
Source: http://drupal.stackexchange.com/questions/4908/how-do-i-add-extra-classe...
This is so very useful,
This is so very useful, bookmarked this page, thanks :)
Sam.
theme or format?
What does that mean for theming?
do you will change theme(apperance) or format( 2013/4/5 or 2013-may-05)?
sorry for bad english.