format_interval

Versions
4.6 – 5
format_interval($timestamp, $granularity = 2)
6 – 7
format_interval($timestamp, $granularity = 2, $langcode = NULL)

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 functions call format_interval()

aggregator_view in modules/aggregator.module
statistics_top_pages in modules/statistics.module
Menu callback; presents the "top pages" page.
statistics_top_referrers in modules/statistics.module
Menu callback; presents the "referrer" page.
statistics_top_visitors in modules/statistics.module
Menu callback; presents the "top visitors" page.
system_cron_settings in modules/system.module
Return the cron status and errors for admin/settings.
theme_aggregator_feed in modules/aggregator.module
Format a news feed.
theme_aggregator_page_item in modules/aggregator.module
Format an individual feed item for display on the aggregator page.
theme_aggregator_summary_item in modules/aggregator.module
Return a themed item heading for summary pages located at "aggregator/sources" and "aggregator/categories".
theme_comment_block in modules/comment.module
tracker_page in modules/tracker.module
Menu callback. Prints a listing of active nodes on the site.
user_admin_account in modules/user.module
user_user in modules/user.module
Implementation of hook_user().
_forum_format in modules/forum.module
Formats a topic for display

Code

includes/common.inc, line 872

<?php
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');
}
?>
Login or register to post comments
 
 

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.