You are here

function twig_date_converter in Translation template extractor 6.3

Same name and namespace in other branches
  1. 7.3 vendor/Twig/Extension/Core.php \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|DateTimeInterface|string|null $date A date:

DateTimeZone|string|null|false $timezone The target timezone, null to use the default, false to leave unchanged:

Return value

DateTime A DateTime instance

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

File

vendor/Twig/Extension/Core.php, line 507

Code

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

  // determine the timezone
  if (false !== $timezone) {
    if (null === $timezone) {
      $timezone = $env
        ->getExtension('core')
        ->getTimezone();
    }
    elseif (!$timezone instanceof DateTimeZone) {
      $timezone = new DateTimeZone($timezone);
    }
  }

  // immutable dates
  if ($date instanceof DateTimeImmutable) {
    return false !== $timezone ? $date
      ->setTimezone($timezone) : $date;
  }
  if ($date instanceof DateTime || $date instanceof DateTimeInterface) {
    $date = clone $date;
    if (false !== $timezone) {
      $date
        ->setTimezone($timezone);
    }
    return $date;
  }
  $asString = (string) $date;
  if (ctype_digit($asString) || !empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1))) {
    $date = '@' . $date;
  }
  $date = new DateTime($date, $env
    ->getExtension('core')
    ->getTimezone());
  if (false !== $timezone) {
    $date
      ->setTimezone($timezone);
  }
  return $date;
}