You are here

function theme_date_time_ago in Date 8

Same name and namespace in other branches
  1. 6.2 theme/theme.inc \theme_date_time_ago()
  2. 7.3 date_api/theme/theme.inc \theme_date_time_ago()
  3. 7 date_api/theme/theme.inc \theme_date_time_ago()
  4. 7.2 date_api/theme/theme.inc \theme_date_time_ago()

Returns HTML for a date in the format 'time ago'.

1 theme call to theme_date_time_ago()
theme_date_display_interval in ./date.theme
Returns HTML for a date element formatted as an interval.

File

date_api/theme/theme.inc, line 75
Theme files for Date API.

Code

function theme_date_time_ago($variables) {
  $start_date = $variables['start_date'];
  $end_date = $variables['end_date'];
  $interval = !empty($variables['interval']) ? $variables['interval'] : 2;
  $display = isset($variables['interval_display']) ? $variables['interval_display'] : 'time ago';

  // If no date is sent, then return nothing.
  if (empty($start_date) || empty($end_date)) {
    return;
  }

  // Time to compare dates to.
  $now = date_format(new DrupalDateTime(), DATE_FORMAT_UNIX);
  $start = date_format($start_date, DATE_FORMAT_UNIX);

  // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence)
  $time_diff = $now - $start;

  // Uses the same options used by Views format_interval.
  switch ($display) {
    case 'raw time ago':
      return format_interval($time_diff, $interval);
    case 'time ago':
      return t('%time ago', array(
        '%time' => format_interval($time_diff, $interval),
      ));
    case 'raw time hence':
      return format_interval(-$time_diff, $interval);
    case 'time hence':
      return t('%time hence', array(
        '%time' => format_interval(-$time_diff, $interval),
      ));
    case 'raw time span':
      return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), $interval);
    case 'inverse time span':
      return ($time_diff > 0 ? '-' : '') . format_interval(abs($time_diff), $interval);
    case 'time span':
      return t($time_diff < 0 ? '%time hence' : '%time ago', array(
        '%time' => format_interval(abs($time_diff), $interval),
      ));
  }
}