Displaying last updated or changed date on a Drupal node

Submitted by fabio on Mon, 2009-07-06 16:41.

Drupal 5.x and 6.x default behavior concerning the visualization of post dates it's pretty simple.

It just display the date when the post has been created. Something like:
Submitted by fabio on Mon, 2009-07-06 17:05.

Well, that's nothing wrong with this if you are used to post and then forget about what you posted. But if you usually keep your posts updated (eg when you created a guide for a software which has been upgraded) then the created date it's pretty useless as no information is displayed on the last update (changes) date.

With the recent Drupal 6 upgrade I completed on this website, I did some little theme tweaks to improve posts date visualization.

Using the following code you will be able to obtain something like:
Last updated on Thu, 2009-03-05 02:15. Originally submitted by fabio on 2007-02-19 17:32.

Obviously, as people usually save the post while still editing (so that they save a copy on the server) or later on the copywriting phase, it's useless displaying a last updated time which is just barely different from the creation time.

We will than define a time unit and a threshold. The code will then just display the last updated informations only if the difference between creation and last update times will exceed the defined threshold.

template.php additions

The first step is adding the following code to the template.php file of your Drupal theme (just create a new file if you don't have one).

function phptemplate_node_submitted($node) {

  $time_unit = 86400; // number of seconds in 1 day => 24 hours * 60 minutes * 60 seconds
  $threshold = 1;

  if ($node->changed && (round(($node->changed - $node->created) / $time_unit) > $threshold)){ // difference between created and changed times > than threshold
    return t('Last updated on @changed. Originally submitted by !username on @created.', array(
      '@changed' => format_date($node->changed, 'medium'),
      '!username' => theme('username', $node),
      '@created' => format_date($node->created, 'small'),
    ));
  }
  else{
    return t('Submitted by !username on @datetime.',
      array(
        '!username' => theme('username', $node),
        '@datetime' => format_date($node->created),
      ));
  }
}

How does this work?

The function above subtracts the values of $node->changed and $node->created which contains respectively node's last update time and creation time (both unix timestamps in seconds).

The resulting value is the number of seconds between the creation time and the last update time. By dividing this with $time_unit and checking if the result is bigger than $threshold it's possible to display the last update time only if the difference between the creation and the last update times will exceed the defined threshold.

Clear caches

Once you added that just navigate to Administer » Site configuration » Performance and issue a Clear cached data. This will let drupal "find" the newly added function.

Use the new post dates informations

As the function you added above is actually a theme override, which overrides the default function used to fill the $submitted variable in the node.tpl.php (and its variations) file, by simply using $submitted you will already have in it the informations of the last updates dates.

Further improvements

If you use Drupal's revisions system, a cooler implementation could be using revisious data to pull out other cool informations such as the author of the last modification and, using the revisions logs, displaying what changes have been made during time to the node.

I don't use revisions myself but I'm interested in hearing from you if you implemented something similar using revisions.

How well does this work?

Submitted by Adam (not verified) on Thu, 2010-01-28 16:44.

Hey, I was looking for a way to add something like this to my website. I have tutorials and stuff for software, which I update every once in a while, and it would be nice if people knew that it is still current.

I'm going to try this after I back up my website, but is there any behavior I should be aware of before I do?

Also, how would add a field like that using the module the other person posted about without having to type in the information manually?

You shouldn't have problems

Submitted by fabio on Thu, 2010-01-28 18:01.

You shouldn't have problems implementing my code snippet. If you do just post here and I'll try to help.

Please note that the snippet will work on any node type on your website. If you need it only for a specific node type tell me and I'll write a little bit modified snippet for that.

I never used the Submitted By module so I can't help you on that. You should ask for help on their issue tracker.

Submitted By

Submitted by NancyDru (not verified) on Sat, 2009-07-11 15:37.

The Submitted By module provides a token that can be used to show this, and you can vary the format by node type.

Yep, I know about that! But

Submitted by fabio on Sat, 2009-07-11 17:13.

Yep, I know about that! But no, I'm not going to install another module to implement this 10 lines code snippet. It wouldn't have a lot of sense given that I know what I'm doing with the code.

Obviously the Submitted By is a great tool for Drupal users which don't want to get their hands dirty with code.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <img> <h2> <h3> <h4> <b>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • You may insert videos with [video:URL]

More information about formatting options