You are here

function timeago_prepare in Timeago 7

Same name and namespace in other branches
  1. 5 timeago.module \timeago_prepare()
  2. 6 timeago.module \timeago_prepare()

Works out the time since the date provided.

Parameters

the date in unix time format:

Return value

the time since today

2 calls to timeago_prepare()
timeago_comment in ./timeago.module
hook_comment implementation
timeago_nodeapi in ./timeago.module
hook_nodeapi implementation

File

./timeago.module, line 42

Code

function timeago_prepare($original, $startdate, $later = false, $round = 3, $maxseconds = 32556926, $dateformat = 'medium') {

  // array of time period chunks
  $chunks = array(
    array(
      60 * 60 * 24 * 365,
      t("year"),
      t("years"),
    ),
    array(
      60 * 60 * 24 * 30,
      t("month"),
      t("months"),
    ),
    array(
      60 * 60 * 24 * 7,
      t("week"),
      t("weeks"),
    ),
    array(
      60 * 60 * 24,
      t("day"),
      t("days"),
    ),
    array(
      60 * 60,
      t("hour"),
      t("hours"),
    ),
    array(
      60,
      t("minute"),
      t("minutes"),
    ),
    array(
      1,
      t("second"),
      t("seconds"),
    ),
  );
  if ($later) {
    $since = $original - $startdate;
  }
  else {
    $since = $startdate - $original;
  }
  $print = '';
  if ($since < $maxseconds) {

    // Loop trough all the chunks
    $times = 1;
    $totaltime = 0;
    for ($i = 0, $j = count($chunks); $i < $j; $i++) {
      $seconds = $chunks[$i][0];
      $name = $chunks[$i][1];
      $pluralname = $chunks[$i][2];
      if (($count = floor(($since - $totaltime) / $seconds)) != 0 && $times <= $round) {
        if ($times == 1) {
          $print .= $count == 1 ? '1 ' . $name : "{$count} {$pluralname}";
        }
        else {
          $print .= $count == 1 ? ', 1 ' . $name : ", {$count} {$pluralname}";
        }
        $times++;
        $totaltime += $count * $seconds;
      }
    }
    if ($later) {
      return t('%a later', array(
        '%a' => $print,
      ));
    }
    else {
      return t('%a ago', array(
        '%a' => $print,
      ));
    }
  }
  else {
    return t('on %a', array(
      '%a' => format_date($original, $dateformat),
    ));
  }
}