You are here

protected function TwigDate::humanize in Bamboo Twig 8.2

Same name and namespace in other branches
  1. 8.5 bamboo_twig_extensions/src/TwigExtension/TwigDate.php \Drupal\bamboo_twig_extensions\TwigExtension\TwigDate::humanize()
  2. 8.3 bamboo_twig_extensions/src/TwigExtension/TwigDate.php \Drupal\bamboo_twig_extensions\TwigExtension\TwigDate::humanize()
  3. 8.4 bamboo_twig_extensions/src/TwigExtension/TwigDate.php \Drupal\bamboo_twig_extensions\TwigExtension\TwigDate::humanize()

Humanize a period of time according the given unit.

Parameters

int $count: The number of @units before/after.

bool $invert: Is 1 if the interval represents a negative time period and 0 otherwise.

string $unit: A unit from y, m, d, h, i, s.

Return value

string Humanized period of time.

1 call to TwigDate::humanize()
TwigDate::diff in bamboo_twig_extensions/src/TwigExtension/TwigDate.php
Filter for converting dates to a time ago string.

File

bamboo_twig_extensions/src/TwigExtension/TwigDate.php, line 106

Class

TwigDate
Provides bridge for Text functions and filters.

Namespace

Drupal\bamboo_twig_extensions\TwigExtension

Code

protected function humanize($count, $invert, $unit) {
  if (intval($count) > 0) {
    if ($invert) {
      return $this
        ->formatPlural(intval($count), 'in @duration @unit', 'in @duration @units', [
        '@duration' => $count,
        '@unit' => $unit,
      ]);
    }
    else {
      return $this
        ->formatPlural(intval($count), '@duration @unit ago', '@duration @units ago', [
        '@duration' => $count,
        '@unit' => $unit,
      ]);
    }
  }
  else {
    if ($invert) {
      return $this
        ->t('in @duration @unit', [
        '@duration' => $count,
        '@unit' => $unit,
      ]);
    }
    else {
      return $this
        ->t('@duration @unit ago', [
        '@duration' => $count,
        '@unit' => $unit,
      ]);
    }
  }
}