I would like to include some php snippet on my home page that displays the date the site was last updated, i.e. had content added or modified.

Is there an easy way to do this? Would I have to do a db query of nodes, sorted by date and then pull the date information from the first entry (hopefully not)?

If anyone has a snippet of code that would achieve this I'd be grateful.

Thanks.

Comments

Lostmonkey’s picture

Anyone? I'm looking for the same thing.

anner’s picture

This is exactly what I'm looking for...anyone?

Rosamunda’s picture

It would be great to add that kind of snippet.

One feature I would like to add (that maybe it´s good just for me): it would be very cool if the snippet could just count the last updated/added date of content types, excluding other site´s changes (ie. adding a module, or a block).

Thanks!

Rosamunda
Buenos Aires | Argentina
www.ligadelconsorcista.org

Rosamunda
Buenos Aires | Argentina

Lostmonkey’s picture

Using the views.module I have been able to make a block listing the latest additions to my site (images). I have choosen to let it count the time since the update, but I believe it's also possible to just use the date.

:-) Mikkel

www.lostmonkey.dk (Block on the top left)

venkat-rk’s picture

You are right. Views is indeed the module to use for this. And, if I am not mistaken there is actually a "Last Updated ..." field that you can set.

maybourne’s picture

Here's what I'm using. Please post if this is wrong or there's a better way. Thanks!

<?php
function last_node_update ($format = 'g:i a') {
 
$result = db_query('SELECT title, changed FROM {node} WHERE status=1 ORDER BY changed DESC LIMIT 1');
  if (
$node = db_fetch_object($result)) return date($format, $node->changed);
  return
'';
}
?>
prameya’s picture

based on maybourne's code... his code didn't work for me, so i modified it.. and now it works... so i thought i'd share

<?php
  $format2
= 'g:i a';
 
$result2 = db_query("SELECT title, changed FROM {node} WHERE status = 1 ORDER BY changed DESC");
  if (
$node = db_fetch_object($result2)) {
     
$output2 .= date($format2, $node->changed);
    }
  print
$output2;
?>
Rosamunda’s picture

Thanks both of you guys!!!!!!!

Rosamunda
Buenos Aires | Argentina
www.ligadelconsorcista.org

Rosamunda
Buenos Aires | Argentina

NancyDru’s picture

First, where does it go?

Second, does this only get updated when pages are updated or added, rather than when comments are added?

Thanks

ridefree’s picture

hey this is great thanks...
unfortunately it only displays the time it was last updated for me and not the actual date... tho maybe this might be different tomorrow? and show the day as well?
will wait and see

NancyDru’s picture

So I tried: $output2 .= format_date($node->changed, 'custom','m/d/Y'); and it works.

Nancy W.
now running 5 sites on Drupal so far
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in Your Database

Andrés Chandía’s picture

Thanks, it worked for me, the only advise for those who want to implement this block is to go to "Administer > Content management > Content types > Page" and once there activate, on the "Default options" field, the "Create new revision" box, in this way if you don't create a new content but modify an older one your "last update" block will be also updated.

@ch

@ch

Andrés Chandía’s picture

how do i do to display the hour also?
@ch

@ch

NancyDru’s picture

Andrés Chandía’s picture

Thanks, your post is very instructive, my "last update stamp" is now in a very nice presentation, i reproduce it here even though is in catalan but any way you can appreciate that it's nicely representated, thanks very much:


Darrera actualització: Dimecres 3 de Octubre de 2007 a les 20:13 hores
© Universitat Pompeu Fabra, Barcelona



Darrera actualització:

<?php
  $format2
= 'g:i a';
 
$result2 = db_query("SELECT title, changed FROM {node} WHERE status = 1 ORDER BY changed DESC");
  if (
$node = db_fetch_object($result2)) {
     
$output2 .= format_date($node->changed, 'custom','l j \de F \de Y \a \le\s H:i \ho\re\s');
    }
  print
$output2;
?>

© Universitat Pompeu Fabra, Barcelona


@ch

@ch

douglaslopezt’s picture

Your code work for me.

And work very well.

Thanks a lot (Andrés Chandía).

richard.e.morton’s picture

I changed

<?php print $submitted?>

to

<?php print $submitted?><?php if ($submitted<>'' and $node->changed): ?> | Last updated <?php print date('j F Y - g:ia', $node->changed); endif; ?>

in order to preserve the ability to turn off submission date in the theme control and to add a consistent date format (compared to what I use i the rest of the site.

Regards

Richard Morton
NaFoF webmaster - Multi-Activity Clubs in the UK
Built with Drupal, Views, Calendar, Suscriptions, TAC

FlemmingLeer’s picture

Hi,

Is there a way to format the date to follow the locale settings from locale.module ?

This

<?php print $submitted?><?php if ($submitted<>'' and $node->changed): ?> | Last updated <?php print date('j F Y - g:ia', $node->changed); endif; ?>

produces January when I only want the native Danish Januar.

I have tried looking through Drupal.org but I'm not a coder and I'm unable to get the right code :/

Any help is greatly appreciated.

Edit:

I managed to find out by myself using format_date instead :)

<div class="submitted"><?php print $submitted?> | Last revised <?php print format_date($changed, $type = 'medium', $format = '', $timezone = NULL) ?></div>

Even a turtle reaches it´s goal...

fax8’s picture

There are cleaner ways to do so.. see http://www.varesano.net/blog/fabio/displaying%20last%20updated%20or%20ch...

There's also a handbook page on this topic: http://drupal.org/node/511642

------------
www.varesano.net - Fabio Varesano Personal Homepage

NancyDru’s picture

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

joeydi’s picture

It might be easier to use the views module to create a block to display that info. Using the default "tracker" view, set it to create a block, display the block as a list, only display 1 node, and remove all of the fields except the date created. This should give a listing of just the creation date of the most recently added content.

NancyDru’s picture

xamount’s picture

and don't forget to add some sorting to the view: sort by last node updated date descending.

darumaki’s picture

Last updated <?php print strftime( "%d.%m.%Y", $node->changed)?>

bdornbush’s picture

This works great, but if $node->changed is null or zero, then the display is "Last updated 12-31-1969" which is not too useful. This seems to be the case for pages like the Contact page. I changed it to:

<?php if ($node->changed): ?>Last updated <?php print strftime( "%m-%d-%Y", $node->changed)?><?php endif; ?>

NancyDru’s picture

IIRC, the node module sets "updated" and "created" to the current time when the node is saved. If you're seeing zero there, I would look into whichever module set the value incorrectly.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

bdornbush’s picture

I have not dug into Drupal that deeply yet, but is a function like Contact a node? I don't think so, which is why the value is not valid.

debonator’s picture

this is exactly what i'm looking for, but i can't figure out where to put the code. a block? the page itself? ideally, i'd like the "last updated" info to appear directly under the page title.

thanks.

NancyDru’s picture

Based on that, I would suggest modifying the template to add that the the "submitted by..." information in your template.

Nancy Dru (formerly Nancy W. until I got married to Drupal)
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

debonator’s picture

now here's a question: how do i have different formats for the different nodes? for example, in forums and on comments, i want to show the usual submitted by name - date. i turned that function on in admin>>themes. on pages and a custom node i called voter_guides, i only want it to show "last updated" information.

i put the following in node.tpl.php and got the last updated information to show:
<span class="submitted"> Last updated <?php print strftime( "%B %d, %Y", $node->changed)?></span>

i don't want any date information *at all* to show on my front page.

so what do i have to do to show certain formats to certain node types?

thanks in advance.
debra

debonator’s picture

    <?php if ($node->type == "voter_guide" || "page"): ?>
    <div class="updated"> Last updated <?php print strftime( "%B %d, %Y", $node->changed)?></div>
  <?php endif; ?>

so i told it to only print last updated information on "voter_guide" OR "page" and that worked just fine.

Gentoo7’s picture

I tried this but my page reads:

Last updated December 31, 1969

it dates in the 60's, what is with that?

NancyDru’s picture

That would indicate that the datestamp you are getting is zero (or null).

NancyDru (formerly Nancy W. until I got married to Drupal)

Gentoo7’s picture

i see, but how can that be fixed?

NancyDru’s picture

Put this in your "node.tpl.php" file to see if the node is actually even present. Since $node->changed is core stuff, I suspect that there is some reason why the node is not actually available to you in your theme.

<?php
print '<pre>';
print_r(get_defined_vars());
print
'</pre>';
?>

NancyDru (formerly Nancy W. until I got married to Drupal)

Gentoo7’s picture

i got a list of info:

Array
(
[file] => themes/sky/node.tpl.php
[variables] => Array
(
[nid] => 5205
[vid] => 5222
[type] => noticias_dia
[status] => 1
[created] => 1229701572
[changed] => 1229701707
[comment] => 2
[promote] => 1
[sticky] => 0
[revision_timestamp] => 1229701707
[title] => GM y Chrysler recibiran $13.4B
[teaser] => 1
[log] =>
[format] => 3
[uid] => 2
[name] => mysite
[picture] =>
[data] => a:3:{s:7:"contact";i:1;s:14:"picture_delete";s:0:"";s:14:"picture_upload";s:0:"";}
[path] => noticias-del-dia/gm-y-chrysler-recibiran-134b
[authorship] =>
[profile_real_name] =>
[last_comment_timestamp] => 1229750725
[last_comment_name] => Manuel
[comment_count] => 1
[taxonomy] => Array
(
)

[files] => Array
(
)

[0] =>
[iid] =>
[readmore] => 1

NancyDru’s picture

$node->changed is there and not zero, so your date formatting must be messed up. Try format_date($node->changed, 'small') to see if that works better.

NancyDru (formerly Nancy W. until I got married to Drupal)

NancyDru’s picture

moreorless’s picture

This is the way I solved this. It may seem quite obvious to old-hands but it took me a while to work it out so I thought I'd share.

I first tried:

<?php
 
print strftime( "%m-%d-%Y", $node->changed)
?>

It worked okay except my host's server time is set to GMT which, over here in Australia, threw the dates out.

I needed a better solution. Here's how I did it - step-by-step.

1. In Site building > Themes > Configure look at the 'Display post information on' box on the right and check the content types you want date updated applied to.

2. Make sure you have a copy of node.tpl.php in your theme directory. You can edit this file to place the print $submitted declaration wherever you want on the page.

I wanted it at the bottom of the page, below the content, so placed the declaration at the bottom of the file.

3. Make sure you have a copy of template.php in your theme directory. Add the code provided at http://drupal.org/node/511642 to the file.

If the phptemplate_node_submitted function is already declared in the file you will either have to delete or comment this existing declaration out.

4. Edit the code provided at http://drupal.org/node/511642 to customise the date modified output.

I wanted a stripped-down output so used this:

<?php
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('@changed', array(
     
'@changed' => format_date($node->changed, 'custom' , 'l, d F Y'),
//      '!username' => theme('username', $node),
//      '@created' => format_date($node->created, 'small'),
   
));
  }
  else{
    return
t('@datetime', array(
//        '!username' => theme('username', $node),
       
'@datetime' => format_date($node->created, 'custom' , 'l, d F Y'),
      ));
  }
}
?>

That's it.

Works well and the dates reflect local time as defined in the Drupal set-up.

Cheers

jtjones23’s picture

Thanks for the instructions, I must be missing something because I can not get "last updated" to display on a post.

I need to get this working on a Drupal 5 site. I'm using MAMP as my dev site and I've tried the garland theme and Newsflash theme. I still see "submitted by" even though the post have been updated and it's more than a day since the post has been created. Any suggestions?

Lynbarn’s picture

Hi,

I'm trying to do something similar to this by displaying in a block, the date/time that a particular node was last updated. Is that possible?

Actually, Ideally, I would prefer to display "updated" in the block when registered users login and they haven't visited the node since the last update, similar to the way news items are flagged.

Regards,

Martyn

FlemmingLeer’s picture

Hi Martyn,

The dashboard here on Drupal.org does that.

I guess it's this one:
http://drupal.org/project/dashboard

But Drupal.org is probably running Drupal 7 now and this module is not a D7, yet.

Maybe there's a post somewhere describing the current setup of Drupal.org ? I searched, but I couldn't find anything.

Even a turtle reaches it´s goal...

jtjones23’s picture

Still Drupal 6 - See http://drupal.org/CHANGELOG.txt

spacereactor’s picture

i using display suite and is very easy to add last edit date.
goto node display and Add new code field with the below code and that it. arrange the field display position with display suite.

<?php
 
print strftime( "%d-%m-%Y", $object->changed)
?>
aeski’s picture

In drupal 7 you can just add the following in node.tpl.php instead of the default $submitted value. I moved mine to underneath the main content.

<?php
 
if ($submitted) {
         echo
'This page was last modified on ' . date( "F j, Y",$node->changed); 
} ;
?>
GinaF’s picture

Sorry, but please could you clarify for a newbie...
Where is the node.tpl.php file? I can't find it. Sorry to be dumb.

Rosamunda’s picture

Hi there,
It´s inside your theme´s folder.
Cheers,
Rosamunda

Rosamunda
Buenos Aires | Argentina

GinaF’s picture

I have looked & looked & feeling dumber by the minute, but it's just not there!
Is it definitely called node.tpl.php in Drupal 7??
EDIT- I've stuck it in page.tpl.php. It's showing the last updated date, but it doesn't recognise the $submitted variable so I had to remove that.

NancyDru’s picture

Which theme are you using?

aalireza’s picture

hi, in drupal 7 how can add time ago format?

new123456789’s picture

Thank you.

Stephen Ollman’s picture

Another very simple way to do this without creating any code is to create a view that grabs the updated timestamp of a node that is captured using the contextual filter and throwing the output into a block view.

This way the view dynamically grabs the last update timestamp from the active node and renders the block. Output the block at the bottom of your content.

Simple!

OPC IT eBusiness

madhukarkr’s picture

You can display a node's last updated date by adding a small code snippet in your theme's page.tpl.php. Though $node is available on every node display, You can check this by using dsm($node), provided you have 'devel' installed.
In this $node->changed will give Unix timestamp for last updated date of particular node. Display this "$node->changed" at a place of your choice in page.tpl.php by formatting it in any date format.
e.g.

<?php
 $date
= date('d-M-Y', $node->changed); print $date;
?>

madhukarkr’s picture

You can display a node's last updated date by adding a small code snippet in your theme's page.tpl.php. Though $node is available on every node display, You can check this by using dsm($node), provided you have 'devel' installed.
In this $node->changed will give Unix timestamp for last updated date of particular node. Display this "$node->changed" at a place of your choice in page.tpl.php by formatting it in any date format.
e.g.

<?php
 $date
= date('d-M-Y', $node->changed); print $date;
?>

NancyDru’s picture

The more correct place to put such a snippet would be in node.tpl.php if you want to do this in the theme.

Personally, I would add it in hook_node_view() where it can be part of the node's output. Even better would be to also add hook_extra_fields() so that the admin can place it according to their preferences.

madhukarkr’s picture

Thank you @NancyDru for pointing this out.
Using "hook_field_extra_fields", this implementation can be done like below:

function mymodule_field_extra_fields() {
    $extra['node']['page'] = array(
    'display' => array(
      'updatedate' => array(
        'label' => t('Update Date'),
        'description' => t('Update Date'),
        'weight' => 0,
      ),
    ),
  );
  return $extra;
}
function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
    if ($type == 'node' && $entity->type == 'page') {
        $extra_fields = field_extra_fields_get_display('node', $entity->type, $view_mode);
        $date = date('d-M-Y', $entity->changed);
        $entity->content['updatedate'] = array(
            '#markup' => '<div class="update-date">Last updated on '.$date.'</div>'
          );
    }
}

Again, entity_view can be replaced by node_view