function 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

32 calls to format_interval()
AggregatorController::adminOverview in drupal/core/modules/aggregator/lib/Drupal/aggregator/Controller/AggregatorController.php
Displays the aggregator administration page.
CommentTokenReplaceTest::testCommentTokenReplacement in drupal/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php
Creates a comment, then tests the tokens generated from it.
ContactPersonalTest::testPersonalContactFlood in drupal/core/modules/contact/lib/Drupal/contact/Tests/ContactPersonalTest.php
Tests the personal contact form flood protection.
ContactSitewideTest::testSiteWideContact in drupal/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php
Tests configuration options and the site-wide contact form.
contact_flood_control in drupal/core/modules/contact/contact.pages.inc
Throws an exception if the current user is not allowed to submit a contact form.

... See full list

7 string references to 'format_interval'
CronForm::buildForm in drupal/core/modules/system/lib/Drupal/system/Form/CronForm.php
Implements \Drupal\Core\Form\FormInterface::buildForm().
DefaultProcessor::settingsForm in drupal/core/modules/aggregator/lib/Drupal/aggregator/Plugin/aggregator/processor/DefaultProcessor.php
Implements \Drupal\aggregator\Plugin\ProcessorInterface::settingsForm().
FeedFormController::form in drupal/core/modules/aggregator/lib/Drupal/aggregator/FeedFormController.php
Overrides Drupal\Core\Entity\EntityFormController::form().
OpmlFeedAdd::buildForm in drupal/core/modules/aggregator/lib/Drupal/aggregator/Form/OpmlFeedAdd.php
Form constructor.
PerformanceForm::buildForm in drupal/core/modules/system/lib/Drupal/system/Form/PerformanceForm.php
Implements \Drupal\Core\Form\FormInterface::buildForm().

... See full list

File

drupal/core/includes/common.inc, line 1220
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,
  ));
}