From the original blog post on Displaying last updated or changed date on a Drupal node by Fabio Varesano (fax8).

By default Drupal insert something like Submitted by fabio on Mon, 2009-07-06 17:05 into the $submitted variable in the node.tpl.php (and variations) file. The date is normally the node creation date.

Sometimes, the node creation date, is just useless: for example in frequently updated pages. It would be more useful displaying the last update date.

Following the procedure to implement this.

Add the following code to your theme template.php file:

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),
      ));
  }
}

If you are running Drupal 6.x, 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.

You can now just use the $submitted variable which will contains the last updated date.

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.

The code above will display last updated date only if there are 1 days between the creation and the last update times.

Alternatives

  • Submited By Module - The Submitted By module provides a token that can be used to show the last updated information. Also, you can vary the format by node type.

Comments

jtjones23’s picture

Is there a Drupal 5 version of this code (this page is marked as a Drupal 5 solution). There's no phptemplate_node_submitted in the Drupal 5 API.