You are here

function timeago_format_date in Timeago 7.2

Same name and namespace in other branches
  1. 6.2 timeago.module \timeago_format_date()

Converts a timestamp into a Timeago date.

Parameters

$timestamp: A UNIX timestamp.

$date: (Optional) A human-readable date (will be displayed if JS is disabled). If not provided, the site default date format is used.

Return value

HTML representing a Timeago-friendly date.

3 calls to timeago_format_date()
timeago_preprocess_comment in ./timeago.module
Implements hook_preprocess_comment().
timeago_process_node in ./timeago.module
Implements hook_process_node().
timeago_tokens in ./timeago.module
Implements hook_tokens().

File

./timeago.module, line 33
Adds support for the Timeago jQuery library.

Code

function timeago_format_date($timestamp, $date = NULL) {

  // Add the Timeago JS.
  timeago_add_js();

  // The fallback date isn't set, so we have to generate it ourselves.
  if (!isset($date)) {

    // If the date format is already set to Timeago, we need to set it to
    // something else or we'll end up with two timeago wrappers.
    $date_format_medium = variable_get('date_format_medium', 'D, m/d/Y - H:i');
    if ($date_format_medium == TIMEAGO_FORMAT_MEDIUM_US) {
      $date_format_medium = 'D, n/j/Y - g:ia';
    }
    elseif ($date_format_medium == TIMEAGO_FORMAT_MEDIUM) {
      $date_format_medium = 'D, d/m/Y - H:i';
    }
    else {
      $date = format_date($timestamp, 'custom', $date_format_medium);
    }
  }
  elseif (strpos($date, 'class="timeago"') !== FALSE) {
    return $date;
  }

  // Construct the Timeago element.
  $elem = variable_get('timeago_elem', 'span');
  $time = format_date($timestamp, 'custom', 'c');
  if ($elem == 'time') {
    return '<time class="timeago" datetime="' . $time . '">' . $date . '</time>';
  }
  else {
    return '<' . $elem . ' class="timeago" title="' . $time . '">' . $date . '</' . $elem . '>';
  }
  return $date;
}