0

I am using Drupal 7, and I would like to include a PHP snippet on my home page that displays the date the site was last updated (i.e., had content added or modified).

How can I go about doing this?

2 Answers 2

4

Assuming you just want to check nodes (and not other content, like users), you'll want to check the node table and find the row with the latest changed timestamp. This function should do the trick:

/**
 * Get the last updated date for nodes.
 *
 * @return
 *   A formatted date for the last updated time if there are published nodes,
 *   FALSE otherwise.
 */
function _mymodule_last_updated() {
  // Pull from cache if available
  if($cache = cache_get('mymodule_last_updated')) {
    $last_updated = $cache->data;
  }
  else {
    // Otherwise, find the timestamp for the latest node record
    $last_updated = db_select('node', 'n')
      ->fields('n', array('changed'))
      ->condition('status', 0, '>') // only published nodes count
      ->orderBy('changed', 'DESC')
      ->range(0, 1)
      ->execute()
      ->fetchField();

    // If nothing found, return FALSE.
    if (!$last_updated) {
      return FALSE;
    }

    // Otherwise, save the data to cache for one hour.
    cache_set('mymodule_last_updated', $last_updated, 'cache', time() + 3600);
  }

  return format_date($last_updated);
}

Now, wherever you wanted to put the information, just call _mymodule_last_updated():

if ($last_update = _mymodule_last_updated()) {
  $last_update_text = t('Last updated: @name', array('@date' => $last_update));
}
else {
  $last_update_text = t('There have been no updates lately.');
}

However, I'd suggest creating custom module and defining a block. This would allow you to avoid having to mess with the PHP code any time you wanted to move the information around, and expose it to others who don't have access to the source code.

Once you've created your custom module, you want to implement the hook_block_info() and hook_block_view() hooks:

/**
 * Implements hook_block_info().
 */
function mymodule_block_info() {
  $blocks['mymodule_last_updated'] = array(
    'info' => t('Last updated block'),
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function mymodule_block_view($delta = '') {
  $block = array();

  // Only act if it's our block
  if ($delta == 'mymodule_last_updated') {
    $block['subject'] = t('Last updated');

    if ($last_update = _mymodule_last_updated()) {
      $block['content'] = t('The site was last updated on @date.', array('@date' => $last_update));
    }
    else {
      $block['content'] = t('There have been no updates.');
    }
  }

  return $block;
}

Once defined, your new block would show up in StructureBlocks.

Note: in all of these snippets, you'll want to replace mymodule with your custom module's name.

3

Use trigger module in the core to keep track all the modify events of your system. And save the time of that event to a global variable by variable_set(). Thus you don't need to query the whole nodes table each time a page viewed. And you can keep track of other entity_type's modification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.