You are here

public function I18n::formatDate in Bamboo Twig 8.2

Same name and namespace in other branches
  1. 8.5 bamboo_twig_i18n/src/TwigExtension/I18n.php \Drupal\bamboo_twig_i18n\TwigExtension\I18n::formatDate()
  2. 8.3 bamboo_twig_i18n/src/TwigExtension/I18n.php \Drupal\bamboo_twig_i18n\TwigExtension\I18n::formatDate()
  3. 8.4 bamboo_twig_i18n/src/TwigExtension/I18n.php \Drupal\bamboo_twig_i18n\TwigExtension\I18n::formatDate()

Render a custom date format with Twig.

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

Parameters

\Drupal\Core\Template\TwigEnvironment $env: A Twig_Environment instance.

int|string|DateTime $date: A string, integer timestamp or DateTime object to convert.

string $type: (optional) The format to use, one of:

  • One of the built-in formats: 'short', 'medium', 'long', 'html_datetime', 'html_date', 'html_time', 'html_yearless_date', 'html_week', 'html_month', 'html_year'.
  • The name of a date type defined by a date format config entity.
  • The machine name of an administrator-defined date format.
  • 'custom', to use $format.

Defaults to 'medium'.

string $format: (optional) If $type is 'custom', a PHP date format string suitable for input to date(). Use a backslash to escape ordinary text, so it does not get interpreted as date format characters.

string|null $timezone: (optional) Time zone identifier, as described at http://php.net/manual/timezones.php Defaults to the time zone used to display the page.

string|null $langcode: (optional) Language code to translate to. NULL (default) means to use the user interface language for the page.

Return value

string|null A translated date string in the requested format. Since the format may contain user input, this value should be escaped when output.

File

bamboo_twig_i18n/src/TwigExtension/I18n.php, line 80

Class

I18n
Provides some 'Internationalization' Twig Extensions.

Namespace

Drupal\bamboo_twig_i18n\TwigExtension

Code

public function formatDate(TwigEnvironment $env, $date, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
  $date = twig_date_converter($env, $date);
  if ($date instanceof \DateTime) {
    return $this
      ->getDateFormatter()
      ->format($date
      ->getTimestamp(), $type, $format, $timezone, $langcode);
  }
  return NULL;
}