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 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 417

Code

function twig_date_converter(Twig_Environment $env, $date = null, $timezone = null) {
  if (!$date instanceof DateTime) {
    $asString = (string) $date;
    if (ctype_digit($asString) || !empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1))) {
      $date = new DateTime('@' . $date);
    }
    else {
      $date = new DateTime($date);
    }
  }
  else {
    $date = clone $date;
  }

  // set Timezone
  if (null !== $timezone) {
    if ($timezone instanceof DateTimeZone) {
      $date
        ->setTimezone($timezone);
    }
    else {
      $date
        ->setTimezone(new DateTimeZone($timezone));
    }
  }
  elseif (($timezone = $env
    ->getExtension('core')
    ->getTimezone()) instanceof DateTimeZone) {
    $date
      ->setTimezone($timezone);
  }
  else {
    $date
      ->setTimezone(new DateTimeZone(date_default_timezone_get()));
  }
  return $date;
}