Community Documentation

format_interval

5 common.inc format_interval($timestamp, $granularity = 2)
6 common.inc format_interval($timestamp, $granularity = 2, $langcode = NULL)
7 common.inc format_interval($interval, $granularity = 2, $langcode = NULL)
8 common.inc format_interval($interval, $granularity = 2, $langcode = NULL)

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 functions call 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.
contact_site_form in modules/contact/contact.pages.inc
Form constructor for the site-wide contact form.
hook_requirements in modules/system/system.api.php
Check installation requirements and do status reporting.
NodeTokenReplaceTestCase::testNodeTokenReplacement in modules/node/node.test
Creates a node, then tests the tokens generated from it.
PollTokenReplaceTestCase::testPollTokenReplacement in modules/poll/poll.test
Creates a poll, then tests the tokens generated from it.
poll_tokens in modules/poll/poll.tokens.inc
Implements hook_tokens().
statistics_top_pages in modules/statistics/statistics.admin.inc
Menu callback; presents the "top pages" page.
statistics_top_referrers in modules/statistics/statistics.admin.inc
Menu callback; presents the "referrer" page.
statistics_top_visitors in modules/statistics/statistics.admin.inc
Menu callback; presents the "top visitors" page.
system_cron_settings in modules/system/system.admin.inc
Form builder; Cron form.
system_requirements in modules/system/system.install
Test and report Drupal installation requirements.
system_tokens in modules/system/system.tokens.inc
Implements hook_tokens().
system_token_info in modules/system/system.tokens.inc
Implements hook_token_info().
template_preprocess_aggregator_feed_source in modules/aggregator/aggregator.pages.inc
Processes variables for aggregator-feed-source.tpl.php.
template_preprocess_aggregator_item in modules/aggregator/aggregator.pages.inc
Processes variables for aggregator-item.tpl.php.
template_preprocess_aggregator_summary_item in modules/aggregator/aggregator.pages.inc
Processes variables for aggregator-summary-item.tpl.php.
template_preprocess_forum_submitted in modules/forum/forum.module
Process variables to format submission info for display in the forum list and topic list.
theme_comment_block in modules/comment/comment.module
Returns HTML for a list of recent comments to be displayed in the comment block.
theme_update_last_check in modules/update/update.module
Returns HTML for the last time we checked for update data.
TokenReplaceTestCase::testSystemDateTokenReplacement in modules/system/system.test
Tests the generation of all system date tokens.
TokenReplaceTestCase::testTokenReplacement in modules/system/system.test
Creates a user and a node, then tests the tokens generated from them.
tracker_page in modules/tracker/tracker.pages.inc
Menu callback; prints a listing of active nodes on the site.
user_admin_account in modules/user/user.admin.inc
Form builder; User administration page.
user_user_view in modules/user/user.module
Implements hook_user_view().
_batch_finished in includes/batch.inc
Ends the batch processing.
_batch_process in includes/batch.inc
Processes sets in a batch.

File

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

Code

<?php
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));
}
?>

Comments

Node posted ago in D6

$ago = format_interval((time() - $node->created) , 2) . t(' ago');

Love the people that make

Love the people that make life easier! Thanks for the share!

For D7

I have used it like this.

<?php
function MYTHEME_preprocess_node(&$variables) {
   
$variables['date'] = 'submmited ' . format_interval((time() - $variables['changed']) , 2) . t(' ago');
}
?>

Perry.

time ago function

i use this in template.php

<?php
function mytheme_ago($changed) {
   
$ago = format_interval((time() - $changed) , 2) . t(' ago');
    return
$ago;
}
?>

then in any template file, call it like this:

<?php
print mytheme_ago($comment->changed);
?>

Translations?

All the comments above are not translatable! There might be some languages where 'ago' is placed before the date.

Always use replacement patterns when using t()!

<?php
t
('@interval ago.', array('@interval' => format_interval((REQUEST_TIME - $timestamp))));
?>

Login or register to post comments