/** * Outputs message depending on flag * * @return string */ public function __invoke(\DateTimeInterface $start, \DateTimeInterface $end, $format = null, \DateTimeZone $timezone = null, $separator = ' - ') { if (null !== $timezone) { $start->setTimezone($timezone); $end->setTimezone($timezone); } if (null !== $format) { $this->format = $format; } if ($start->format('Y') != $end->format('Y')) { return $start->format('d.m.Y') . $separator . $end->format('d.m.Y'); } if ($start->format('m') != $end->format('m')) { return $start->format('d.m.') . $separator . $end->format('d.m.Y'); } if ($start->format('d') != $end->format('d')) { return $start->format('d.') . $separator . $end->format('d.m.Y'); } if ($start->format('H') != $end->format('H') || $start->format('i') != $end->format('i')) { return $start->format('d.m.Y H:i') . $separator . $end->format('H:i'); } return ''; }
/** * Converts an input to a DateTime instance. * * <pre> * {% if date(user.created_at) < date('+2days') %} * {# do something #} * {% endif %} * </pre> * * @param Twig_Environment $env A Twig_Environment instance * @param DateTime|DateTimeInterface|string|null $date A date * @param DateTimeZone|string|null|false $timezone The target timezone, null to use the default, false to leave unchanged * * @return DateTime A DateTime instance */ 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; }
public function convertToDatabase(\DateTimeInterface $value) : string { return $value->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i:s'); }