You are here

public function Carbon::diffForHumans in Persian Date for Drupal 8 8.4

Get the difference in a human readable format in the current locale.

When comparing a value in the past to default now: 1 hour ago 5 months ago

When comparing a value in the future to default now: 1 hour from now 5 months from now

When comparing a value in the past to another value: 1 hour before 5 months before

When comparing a value in the future to another value: 1 hour after 5 months after

Parameters

Carbon|null $other:

bool $absolute removes time difference modifiers ago, after, etc:

bool $short displays short format of time units:

Return value

string

File

src/Library/Carbon/Carbon.php, line 2804

Class

Carbon
A simple API extension for DateTime

Namespace

Drupal\persian_date\Library\Carbon

Code

public function diffForHumans(Carbon $other = null, $absolute = false, $short = false) {
  $isNow = $other === null;
  if ($isNow) {
    $other = static::now($this
      ->getTimezone());
  }
  $diffInterval = $this
    ->diff($other);
  switch (true) {
    case $diffInterval->y > 0:
      $unit = $short ? 'y' : 'year';
      $count = $diffInterval->y;
      break;
    case $diffInterval->m > 0:
      $unit = $short ? 'm' : 'month';
      $count = $diffInterval->m;
      break;
    case $diffInterval->d > 0:
      $unit = $short ? 'd' : 'day';
      $count = $diffInterval->d;
      if ($count >= static::DAYS_PER_WEEK) {
        $unit = $short ? 'w' : 'week';
        $count = (int) ($count / static::DAYS_PER_WEEK);
      }
      break;
    case $diffInterval->h > 0:
      $unit = $short ? 'h' : 'hour';
      $count = $diffInterval->h;
      break;
    case $diffInterval->i > 0:
      $unit = $short ? 'min' : 'minute';
      $count = $diffInterval->i;
      break;
    default:
      $count = $diffInterval->s;
      $unit = $short ? 's' : 'second';
      break;
  }
  if ($count === 0) {
    $count = 1;
  }
  $time = static::translator()
    ->transChoice($unit, $count, array(
    ':count' => $count,
  ));
  if ($absolute) {
    return $time;
  }
  $isFuture = $diffInterval->invert === 1;
  $transId = $isNow ? $isFuture ? 'from_now' : 'ago' : ($isFuture ? 'after' : 'before');

  // Some langs have special pluralization for past and future tense.
  $tryKeyExists = $unit . '_' . $transId;
  if ($tryKeyExists !== static::translator()
    ->transChoice($tryKeyExists, $count)) {
    $time = static::translator()
      ->transChoice($tryKeyExists, $count, array(
      ':count' => $count,
    ));
  }
  return static::translator()
    ->trans($transId, array(
    ':time' => $time,
  ));
}