Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

No time of day in node date

Parent Feed: 

It is common to wish to remove the time of day shown in the node “submitted” line. In Drupal 5, there were no less than three ways to achieve this, and I will present these when I'm done covering date formatting for Drupal 6. Drupal 6 allows you to modify the different formats for dates in the Administration area of your site, these different formats being small, medium and large — yep, just like pizza, oh... my... god... You could do the same in Drupal 5. However, you then had to pick among a finite list of options. In Drupal 6, you can create an nth option, if you like none of the options presented to you.

It is the medium-size date format that's used to format the submitted line in nodes, as shown in lines 2449-2460 of modules/node/node.module:

/**
 * Format the "Submitted by username on date/time" for each node
 *
 * @ingroup themeable
 */
function theme_node_submitted($node) {
  return t('Submitted by !username on @datetime',
    array(
      '!username' => theme('username', $node),
      '@datetime' => format_date($node->created),
    ));
}

In Drupal 6, the signature of the Drupal function format_date() is:

format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)

When the 2nd parameter is not set, 'medium' is used to format the date, and the function theme_node_submitted() is not setting that 2nd parameter when calling format_date().

To modify the medium-size format for dates, head over to the page admin/settings/date-time of your Drupal site. Under Formatting, in the drop-down list of options for “Medium date format:”, you will find a “Custom format” option. Select it. There was no such option in Drupal 5. Note that in Drupal 6 — just like in Drupal 5 — all the preset options still show time of day for the medium-size date.

Look for the preview of the current date in the text below the field on the right, where it says: This format is currently set to display as...

(Take note also that a link to the PHP manual is provided to you, to show you how to format dates in PHP.)

If all you want to do is remove the time of day from your current default, get rid of the - H:i part. Then do a Save configuration.

Now if you look at any node on your site, you will see that the change has taken effect immediately: no more time of day.

If you do not see the change, it can mean either of these two things: your theme is overriding the function theme_node_submitted(), or, the function format_date() is called within your node template file, in your theme, with some custom format, to display the date.

In Drupal 5

In Drupal 5, there were at least three ways of going about changing your date format.

  1. function _phptemplate_variables($hook, $vars) {
      switch($hook) {
        case 'node' :
          $vars['submitted'] = t('Submitted by !username on @date', 
            array(
              '!username' => $vars['name'],
              '@date' => format_date($vars['node']->created, 
                                      'custom', 'l, j M Y'),
            ));
          break;
      }
      return $vars;
    }
  2. Modify your site's settings.php file (http://drupal.org/node/134990#comment-221129) — to change your system's default medium format. Thank you, Mooffie. So you un-comment the $conf array and add one line to it, like so:

    $conf = array(
    #   'site_name' => 'My Drupal site',
    #   'theme_default' => 'minnelli',
    #   'anonymous' => 'Visitor',
        'date_format_medium' => 'D, m/d/Y',
    );
  3. Modify node.tpl.php (http://drupal.org/node/134990#comment-221973) — instead of printing $submitted, you print $node->created formatted. Like so, for example:

    print format_date($node->created, 'custom', 'l, j M Y');

Browse this article

Last edited by Caroline Schnapp about 5 years ago.

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web