You are here

function twig_date_converter in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/twig/twig/lib/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/twig/lib/Twig/Extension/Core.php
Returns a new date object modified.
1 string reference to 'twig_date_converter'
Twig_Extension_Core::getFunctions in vendor/twig/twig/lib/Twig/Extension/Core.php

File

vendor/twig/twig/lib/Twig/Extension/Core.php, line 482

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;
  }
  if (null === $date || 'now' === $date) {
    return new DateTime($date, false !== $timezone ? $timezone : $env
      ->getExtension('core')
      ->getTimezone());
  }
  $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, $env
      ->getExtension('core')
      ->getTimezone());
  }
  if (false !== $timezone) {
    $date
      ->setTimezone($timezone);
  }
  return $date;
}