You are here

function format_interval in Drupal 5

Same name and namespace in other branches
  1. 4 includes/common.inc \format_interval()
  2. 6 includes/common.inc \format_interval()
  3. 7 includes/common.inc \format_interval()

Format a time interval with the requested granularity.

Parameters

$timestamp: The length of the interval in seconds.

$granularity: How many different units to display in the string.

Return value

A translated string representation of the interval.

Related topics

13 calls to format_interval()
aggregator_view in modules/aggregator/aggregator.module
statistics_top_pages in modules/statistics/statistics.module
Menu callback; presents the "top pages" page.
statistics_top_referrers in modules/statistics/statistics.module
Menu callback; presents the "referrer" page.
statistics_top_visitors in modules/statistics/statistics.module
Menu callback; presents the "top visitors" page.
system_requirements in modules/system/system.install
Test and report Drupal installation requirements.

... See full list

7 string references to 'format_interval'
aggregator_admin_settings in modules/aggregator/aggregator.module
aggregator_form_feed in modules/aggregator/aggregator.module
Generate a form to add/edit feed sources.
poll_form in modules/poll/poll.module
Implementation of hook_form().
statistics_access_logging_settings in modules/statistics/statistics.module
system_error_reporting_settings in modules/system/system.module

... See full list

File

includes/common.inc, line 1170
Common functions that many Drupal modules will need to reference.

Code

function format_interval($timestamp, $granularity = 2) {
  $units = array(
    '1 year|@count years' => 31536000,
    '1 week|@count weeks' => 604800,
    '1 day|@count days' => 86400,
    '1 hour|@count hours' => 3600,
    '1 min|@count min' => 60,
    '1 sec|@count sec' => 1,
  );
  $output = '';
  foreach ($units as $key => $value) {
    $key = explode('|', $key);
    if ($timestamp >= $value) {
      $output .= ($output ? ' ' : '') . format_plural(floor($timestamp / $value), $key[0], $key[1]);
      $timestamp %= $value;
      $granularity--;
    }
    if ($granularity == 0) {
      break;
    }
  }
  return $output ? $output : t('0 sec');
}