format_interval

Definition

format_interval($timestamp, $granularity = 2, $langcode = NULL)
includes/common.inc, line 1118

Description

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.

$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

Namesort iconDescription
FormattingFunctions to format numbers, strings, dates, etc.

Code

<?php
function format_interval($timestamp, $granularity = 2, $langcode = NULL) {
  $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], array(), $langcode);
      $timestamp %= $value;
      $granularity--;
    }

    if ($granularity == 0) {
      break;
    }
  }
  return $output ? $output : t('0 sec', array(), $langcode);
}
?>
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.