You are here

function date_format_interval in Date 7.3

Same name and namespace in other branches
  1. 8 date_api/date_api.module \date_format_interval()
  2. 5.2 date_api.module \date_format_interval()
  3. 6.2 date_api.module \date_format_interval()
  4. 6 date_api.module \date_format_interval()
  5. 7 date_api/date_api.module \date_format_interval()
  6. 7.2 date_api/date_api.module \date_format_interval()

Formats a time interval with granularity, including past and future context.

Parameters

object $date: The current date object.

int $granularity: (optional) Number of units to display in the string. Defaults to 2.

Return value

string A translated string representation of the interval.

See also

format_interval()

1 call to date_format_interval()
date_formatter_process in ./date.module
Helper function for creating formatted date arrays from a formatter.

File

date_api/date_api.module, line 1891
This module will make the date API available to other modules.

Code

function date_format_interval($date, $granularity = 2, $display_ago = TRUE) {

  // If no date is sent, then return nothing.
  if (empty($date)) {
    return NULL;
  }
  $interval = REQUEST_TIME - $date
    ->format('U');
  if ($interval > 0) {
    return $display_ago ? t('!time ago', array(
      '!time' => format_interval($interval, $granularity),
    )) : t('!time', array(
      '!time' => format_interval($interval, $granularity),
    ));
  }
  else {
    return format_interval(abs($interval), $granularity);
  }
}