PHPプログラムに関する各種メモ書き

Twigで日付から日本語の曜日を返すフィルター

Twig/Extensionuser にファイル名 Youbi.php で保存します。

Youbi.php

<?php

class Twig_Extensionuser_Youbi extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            'youbi' => new \Twig_Filter_Method($this, 'youbi', array(
                'needs_environment' => true,
                'needs_context' => true,
                'is_safe' => array(
                    'evaluate' => true,
                ),
            )),
        );
    }

    public function youbi(\Twig_Environment $environment, $context, $string1)
    {
        $date = new DateTime($string1);
        $w_no = $date->format('w');
        $week = array('日', '月', '火', '水', '木', '金', '土');
        return $week[$w_no];
    }

    protected function parseString(\Twig_Environment $environment, $context, $string)
    {
        $environment->setLoader(new \Twig_Loader_String());
        return $environment->render($string, $context);
    }

    public function getName()
    {
        return 'youbi';
    }
}

● 使い方

{{'2017-08-04' | youbi }}
No.1162
08/04 10:49

edit