本文整理匯總了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');
}