You are here

protected function TwigDate::getIntervalUnits in Bamboo Twig 8.3

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

Retrieve the diff between two dates for the given unit.

Parameters

\DateInterval $diff: The diff between two dates.

string $unit: The unit that we want to retreive diff.

Return value

float The differences for the given unit.

1 call to TwigDate::getIntervalUnits()
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 159

Class

TwigDate
Provides bridge for Text functions and filters.

Namespace

Drupal\bamboo_twig_extensions\TwigExtension

Code

protected function getIntervalUnits(\DateInterval $diff, $unit) {
  $total = 0;
  switch ($unit) {
    case 'y':
      $total = ($diff->days + $diff->h / 24) / 365.25;
      break;
    case 'm':
      $total = ($diff->days + $diff->h / 24) / 30;
      break;
    case 'd':
      $total = $diff->days + ($diff->h + $diff->i / 60) / 24;
      break;
    case 'h':
      $total = $diff->days * 24 + $diff->h + $diff->i / 60;
      break;
    case 'i':
      $total = ($diff->days * 24 + $diff->h) * 60 + $diff->i + $diff->s / 60;
      break;
    case 's':
      $total = (($diff->days * 24 + $diff->h) * 60 + $diff->i) * 60 + $diff->s;
      break;
  }
  return $total;
}