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

visuaLatte’s picture

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:

<?php if ($submitted) { ?>
    <span class="submitted">
        <?php
            print 'by ' . theme('username', $node) . ' | ' . format_date($node->created, 'custom', 'd/m/Y');
            if ($terms) {
                print ' | in ';
            }
        ?>
    </span>
    <?php } ?>

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?

Jeff Burnz’s picture

You can do something like this:

  <?php if ($display_submitted): ?>
    <span class="submitted">
      <?php
	    $custom_date = format_date($node->created, 'custom', 'l, d/m/Y');
        print t('!username | !datetime', array('!username' => $name, '!datetime' => $custom_date));
      ?>
    </span>
  <?php endif; ?>

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.

ItangSanjana’s picture

@nateeanes & @Jeff Burnz, thanks for the info, very useful ..

Anonymous’s picture

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?

ItangSanjana’s picture

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

langweer’s picture

It may be obvious to experienced users... but I couldn't come up with this.
Thank you!

wickwood’s picture

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!

ItangSanjana’s picture

No, not hack :) medium format is Drupal default for $variables['date'] in node. CMIIW

knalstaaf’s picture

  1. You need to install token module (latest beta version includes field tokens) and custom formatters module from http://drupal.org/project/custom_formatters.
  2. Then enable it from modules page.
  3. Go to admin->structure->formatters.
  4. Add a new formatter, put it a name that you like.
  5. Format: HTML + Tokens
  6. Field type: Date
  7. Formatter:
    <span class="day">[node:source:field-datefieldname:custom:j]</span>
    <span class="month">[node:source:field-datefieldname:custom:F]</span>
    <span class="year">[node:source:field-datefieldname:custom:Y]</span>
  8. Save

Now, you have created a custom date formatter, that you may use at your view!

Source: http://drupal.stackexchange.com/questions/4908/how-do-i-add-extra-classe...

samwillc’s picture

This is so very useful, bookmarked this page, thanks :)

Sam.

namjoo’s picture

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.