php - DateTime::createFromFormat(...) 有替代品吗?

标签 php date twig

我有一个代表周数和年份的字符串,我想输出本周的星期日。

PHP 示例:

$dateString = '502016';
$dateObject = DateTime::createFromFormat('WY', $dateString);
echo $dateObject->format('Y-m-d');

类似于:

{{ published_at|format(date("Ymd"), 'd-m-Y') }}

这在 Twig 中可能吗?

最佳答案

我找到了可以帮助您的东西:overriding the built in twig date filter

它正在为你想要的东西创建一个新的 Twig 扩展:)

PS: 最后它返回一个 PHP DateTime

<?php
/*
 * Extension to provide updated date & date_modify filters along with an
 * updated date function which do not auto-convert strings of numbers to
 * a unix timestamp.
 *
 * Code within dateFilter(), modifyFilter() and dateFromString() extracted
 * from Twig/lib/Twig/Extension/Core.php which is (c) 2009 Fabien Potencier
 * and licensed as per https://github.com/twigphp/Twig/blob/master/LICENSE
 */
namespace My\Twig\Extension;

use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
use DateTime;
use DateTimeInterface;
use DateTimeImmutable;

class DateExtension extends Twig_Extension
{
    public function getName()
    {
        return 'my_date';
    }

    public function getFilters()
    {
        return array(
            new Twig_SimpleFilter('date', [$this, 'dateFilter'],
                    ['needs_environment' => true]),
            new Twig_SimpleFilter('date_modify', [$this, 'modifyFilter'],
                    ['needs_environment' => true]),
        );
    }

    public function getFunctions()
    {
        return array(
            new Twig_SimpleFunction('date', [$this, 'dateFromString'],
                    ['needs_environment' => true]),
        );
    }

    public function dateFilter($env, $date, $format = null, $timezone = null)
    {
        if (null === $format) {
            $formats = $env->getExtension('core')->getDateFormat();
            $format = $date instanceof DateInterval ? $formats[1] : $formats[0];
        }

        if ($date instanceof DateInterval) {
            return $date->format($format);
        }

        return $this->dateFromString($env, $date, $timezone)->format($format);
    }

    public function modifyFilter($env, $date, $format = null, $timezone = null)
    {
        $date = $this->dateFromString($env, $date, false);
        $date->modify($modifier);

        return $date;
    }

    public function dateFromString($env, $date, $timezone)
    {
        // determine the timezone
        if (!$timezone) {
            $defaultTimezone = $env->getExtension('core')->getTimezone();
        } elseif (!$timezone instanceof DateTimeZone) {
            $defaultTimezone = new DateTimeZone($timezone);
        } else {
            $defaultTimezone = $timezone;
        }

        // immutable dates
        if ($date instanceof DateTimeImmutable) {
            return false !== $timezone ? $date->setTimezone($defaultTimezone) : $date;
        }

        if ($date instanceof DateTime || $date instanceof DateTimeInterface) {
            $date = clone $date;
            if (false !== $timezone) {
                $date->setTimezone($defaultTimezone);
            }

            return $date;
        }

        $date = new DateTime($date, $defaultTimezone);
        if (false !== $timezone) {
            $date->setTimezone($defaultTimezone);
        }

        return $date;
    }
}

这个类简单地注册新的日期、date_modify 过滤器和一个新的 date 函数来替换 Twig 核心中的那些,然后是一个直接的 函数 twig_date_format_filter、twig_date_modify_filter 的副本 以及删除了上述功能的 twig_date_converter。

我还需要在 Twig_Environment 中注册这个扩展 使用:

$env->addExtension(new \My\Twig\Extension\DateExtension());

我完成了。

{{ "20141216"|date('jS F Y') }}
{{ "20141216"|date('jS F Y') }}

现在正确输出:2014 年 12 月 16 日 2014 年 12 月 16 日

可惜我不能 覆盖twig_date_converter,很高兴我可以重新注册 相关的 Twig 过滤器和函数。

关于php - DateTime::createFromFormat(...) 有替代品吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40380943/

相关文章:

javascript - 正则表达式解析 ISO-8601

php - 当数组反转时,几个 div 会移开,从而破坏布局

php - Symfony Twig Extension 破坏了其他服务 - 模板是否在安全之前完成?

php - 在 Wordpress/Timber 中获取当前类别 slug

php - 我如何在 yii-booster bootstrap 小部件中制作一个 gridView,其中一个列中的按钮弹出窗口

php - 我的 for 语句有什么问题;得到 undefined offset ?

SQLite 日期 "is greater than other date"例如 '25/09/2014' 不起作用

python - 将日期转换为一年中的某一天,而不使用日期时间

Php检查字符串是否包含多个单词

php - SilverShop 模块的动态排序选项