function twig_date_converter

Converts an input to a DateTime instance.

<pre> {% if date(user.created_at) < date('+2days') %} {# do something #} {% endif %} </pre>

Parameters

Twig_Environment $env A Twig_Environment instance:

DateTime|string $date A date:

DateTimeZone|string $timezone A timezone:

Return value

DateTime A DateTime instance

1 call to twig_date_converter()
twig_date_modify_filter in drupal/core/vendor/twig/twig/lib/Twig/Extension/Core.php
Returns a new date object modified
1 string reference to 'twig_date_converter'
Twig_Extension_Core::getFunctions in drupal/core/vendor/twig/twig/lib/Twig/Extension/Core.php
Returns a list of global functions to add to the existing list.

File

drupal/core/vendor/twig/twig/lib/Twig/Extension/Core.php, line 451

Code

function twig_date_converter(Twig_Environment $env, $date = null, $timezone = null) {

  // determine the timezone
  if (!$timezone) {
    $defaultTimezone = $env
      ->getExtension('core')
      ->getTimezone();
  }
  elseif (!$timezone instanceof DateTimeZone) {
    $defaultTimezone = new DateTimeZone($timezone);
  }
  else {
    $defaultTimezone = $timezone;
  }
  if ($date instanceof DateTime) {
    $date = clone $date;
    if (false !== $timezone) {
      $date
        ->setTimezone($defaultTimezone);
    }
    return $date;
  }
  $asString = (string) $date;
  if (ctype_digit($asString) || !empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1))) {
    $date = '@' . $date;
  }
  $date = new DateTime($date, $defaultTimezone);
  if (false !== $timezone) {
    $date
      ->setTimezone($defaultTimezone);
  }
  return $date;
}