You are here

function format_interval in Drupal 7

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

Formats a time interval with the requested granularity.

Parameters

$interval: The length of the interval in seconds.

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

$langcode: Optional language code to translate to a language other than what is used to display the page.

Return value

A translated string representation of the interval.

Related topics

30 calls to format_interval()
aggregator_view in modules/aggregator/aggregator.admin.inc
Displays the aggregator administration page.
CommentTokenReplaceTestCase::testCommentTokenReplacement in modules/comment/comment.test
Creates a comment, then tests the tokens generated from it.
ContactPersonalTestCase::testPersonalContactFlood in modules/contact/contact.test
Tests the personal contact form flood protection.
ContactSitewideTestCase::testSiteWideContact in modules/contact/contact.test
Tests configuration options and the site-wide contact form.
contact_personal_form in modules/contact/contact.pages.inc
Form constructor for the personal contact form.

... See full list

8 string references to 'format_interval'
aggregator_form_aggregator_admin_form_alter in modules/aggregator/aggregator.processor.inc
Implements hook_form_aggregator_admin_form_alter().
aggregator_form_feed in modules/aggregator/aggregator.admin.inc
Form constructor for adding and editing feed sources.
aggregator_form_opml in modules/aggregator/aggregator.admin.inc
Form constructor for importing feeds from OPML.
poll_form in modules/poll/poll.module
Implements hook_form().
statistics_settings_form in modules/statistics/statistics.admin.inc
Form constructor for the statistics administration form.

... See full list

File

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

Code

function format_interval($interval, $granularity = 2, $langcode = NULL) {
  $units = array(
    '1 year|@count years' => 31536000,
    '1 month|@count months' => 2592000,
    '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 ($interval >= $value) {
      $output .= ($output ? ' ' : '') . format_plural(floor($interval / $value), $key[0], $key[1], array(), array(
        'langcode' => $langcode,
      ));
      $interval %= $value;
      $granularity--;
    }
    if ($granularity == 0) {
      break;
    }
  }
  return $output ? $output : t('0 sec', array(), array(
    'langcode' => $langcode,
  ));
}