You are here

public function Dates::dateFormati18n in Bamboo Twig 8

Render a custom date format with Twig.

Use the internal helper "format_date" to render the date using the current language for texts.

Parameters

mixed $date: A valide DrupalDateTime or DateTime object, a Y-m-d string or an integer timestamp.

string $format: A format string.

Return value

string|null Formatted given date corresponding of given format date.

File

src/TwigExtension/Dates.php, line 58

Class

Dates
Provides a 'Dates' Twig Extensions.

Namespace

Drupal\bamboo_twig\TwigExtension

Code

public function dateFormati18n($date, $format = 'Y-m-d') {
  if (is_a($date, 'Drupal\\Core\\Datetime\\DrupalDateTime') || is_a($date, 'DateTime')) {
    $timestmap = $date
      ->getTimestamp();
  }
  elseif (\DateTime::createFromFormat('Y-m-d', $date)) {
    $timestmap = strtotime($date);
  }
  else {
    $timestmap = $date;
  }

  // Check the $date is a valid timestmap.
  try {
    $date_format = new \DateTime('@' . $timestmap);
    $timestmap = $date_format
      ->getTimestamp();
  } catch (\Exception $e) {
    return NULL;
  }
  return $this->dateFormatter
    ->format($timestmap, "custom", $format);
}