You are here

function socialfeed_time_elapsed_string in Social Feed 7.2

Same name and namespace in other branches
  1. 8 socialfeed.theme.inc \socialfeed_time_elapsed_string()

Displays date in Twitter format.

Parameters

string $datetime: Accepts string as value.

bool $full: Accepts bool as value.

Return value

string Returns string.

1 call to socialfeed_time_elapsed_string()
socialfeed_twitter_posts in ./socialfeed.block.inc
Uses socialfeed_twitter_posts() for fetching Twitter tweets.

File

./socialfeed.block.inc, line 371
File include for Social Feed module.

Code

function socialfeed_time_elapsed_string($datetime, $full = FALSE) {
  $now = new DateTime();
  $ago = new DateTime($datetime);
  $diff = $now
    ->diff($ago);
  $diff->w = floor($diff->d / 7);
  $diff->d -= $diff->w * 7;
  $string = [
    'y' => 'year',
    'm' => 'month',
    'w' => 'week',
    'd' => 'day',
    'h' => 'hour',
    'i' => 'minute',
    's' => 'second',
  ];
  foreach ($string as $k => &$v) {
    if ($diff->{$k}) {
      $v = $diff->{$k} . ' ' . t($v . ($diff->{$k} > 1 ? 's' : ''));
    }
    else {
      unset($string[$k]);
    }
  }
  if (!$full) {
    $string = array_slice($string, 0, 1);
  }
  return $string ? t("@time ago", [
    '@time' => implode(', ', $string),
  ]) : t('just now');
}