本文整理汇总了PHP中DateTimeInterface::setTimezone方法的典型用法代码示例。如果您正苦于以下问题:PHP DateTimeInterface::setTimezone方法的具体用法?PHP DateTimeInterface::setTimezone怎么用?PHP DateTimeInterface::setTimezone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateTimeInterface
的用法示例。
在下文中一共展示了DateTimeInterface::setTimezone方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __invoke
/**
* 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 '';
}
示例2: twig_date_converter
/**
* 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;
}
示例3: convertToDatabase
public function convertToDatabase(\DateTimeInterface $value) : string
{
return $value->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i:s');
}