You are here

protected function TwigDate::humanize in Bamboo Twig 8.4

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.2 bamboo_twig_extensions/src/TwigExtension/TwigDate.php \Drupal\bamboo_twig_extensions\TwigExtension\TwigDate::humanize()
  3. 8.3 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 year, minute, day, hour, minute, second.

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 107

Class

TwigDate
Provides bridge for Text functions and filters.

Namespace

Drupal\bamboo_twig_extensions\TwigExtension

Code

protected function humanize($count, $invert, $unit) {

  // Get singular translatable unit of time.
  $t_unit = $this
    ->t($unit, [], [
    'context' => 'Time difference unit',
  ]);

  // Get plural translatable unit of time.
  $t_units = $this
    ->t($unit . 's', [], [
    'context' => 'Time difference unit',
  ]);

  // Don't generate pluralized strings when count less than 0.
  if ((int) $count <= 0) {
    if ($invert) {
      return $this
        ->t('in @duration @unit', [
        '@duration' => $count,
        '@unit' => $t_unit,
      ], [
        'context' => 'Time difference',
      ]);
    }
    return $this
      ->t('@duration @unit ago', [
      '@duration' => $count,
      '@unit' => $t_unit,
    ], [
      'context' => 'Time difference',
    ]);
  }

  // From here, we need to humanize a potential plural Time difference.
  if ($invert) {
    return $this
      ->formatPlural((int) $count, 'in @duration @unit', 'in @duration @units', [
      '@duration' => $count,
      '@unit' => $t_unit,
      '@units' => $t_units,
    ], [
      'context' => 'Time difference',
    ]);
  }
  return $this
    ->formatPlural((int) $count, '@duration @unit ago', '@duration @units ago', [
    '@duration' => $count,
    '@unit' => $t_unit,
    '@units' => $t_units,
  ], [
    'context' => 'Time difference',
  ]);
}