2

日付形式を次のように表示したい10月09日, 周三(つまり、10/9、水曜日を意味します)。データは [message.time]: の よう
な UNIX 時間 です。 w" は数字であり、中国語のテキストではありません。 テキスト形式を変換するためにTwigで何かできるでしょうか?1380813820000


{{ (message.time/1000)|date("m月d日, 周w") }}
10月09日,周3

ありがとう

4

3 に答える 3

2

根本的な問題は、Twig がDateTime::formatロケールや (私の知る限り) 曜日の名前を変換する他のタイプの機能をサポートしないメソッドを使用することです。

次の 3 つの解決策があります。

  • strftimeロケール (およびローカライズされた曜日名) をサポートする を使用します。
  • intlPHPの拡張機能を使用できる場合は、Twig の拡張機能が付属しているTwig-extensionsintlを使用できます。
  • 曜日は自分で翻訳します。

さらに、Twig テンプレートで好みのソリューションを使用するには、Twig の機能を拡張する必要があります。

strftime と setlocale の使用

次の (かなり大きな) コードは、strftimeソリューションを実装しています。

<?php

// inspired by phpdude:
// https://github.com/fabpot/twig/issues/378#issuecomment-4698225
class DateTimeHelper_Twig extends Twig_Extension
{
    public function getFilters()
    {
        return array(
            'datetime' => new Twig_Filter_Method($this, 'datetime',
                array('needs_environment' => true)),
        );
    }

    // This uses `strftime` which makes use of the locale. The format is not
    // compatible with the one of date() or DateTime::format().
    public function datetime(Twig_Environment $env, $date,
                             $format = "%B %e, %Y %H:%M", $timezone = null)
    {
        $date = twig_date_converter($env, $date, $timezone);

        return strftime($format, $date->getTimestamp());
    }

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

$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader);

// Call the setlocale before you use the `datetime` in your templates.
// This only needs to be done once per request.
// If you already have a locale configured in your environment, 
// you can replace this with setlocale(LC_TIME, ""); - that way the
// locale of your environment is used.
setlocale(LC_TIME, "zh_CN.UTF-8");

// Add the extension to Twig like that:
$twig->addExtension(new DateTimeHelper_Twig());

$message = array('time' => time() * 1000);

// use the `datetime` filter with %a which gets replaced by the short weekday name of
// the current locale.
echo $twig->render('{{ (message.time/1000)|datetime("%m月%d日, 周%a") }}',
    array('message' => $message)), PHP_EOL;

このコードは10月09日, 周三私のシステムに表示されます (debian パッケージをインストールした後locales-all;-))。

もちろん、ロケールには、おそらく注意が必要な制限のリストがあります。

  • 正しいロケール (おそらく UTF-8) を使用する必要があり、コードを使用するすべてのシステムに必要なロケールをインストールする必要があります。
  • また、このソリューションはプラットフォームに完全に依存しているわけではありません (Windows ではsetlocale動作が異なり、結果が異なります)。のPHPマニュアルをチェックしてくださいsetlocale
  • 物事を台無しにするのは簡単です。

intl と Twig 拡張機能の使用

intl拡張機能と「Twig-extensions」パッケージを使用できる場合は、代わりに使用する必要がありlocalizeddateますdate:

// add the extension like that
$twig->addExtension(new Twig_Extensions_Extension_Intl());

$message = array('time' => time() * 1000);
echo $twig->render('{{ (message.time/1000)|localizeddate("none", "none", "zh", null, "MM月dd日, eee") }}', array('message' => $message)), PHP_EOL;

そのコードも示しています10月09日, 周三-それは周-thingieを自動的に追加します.

もちろん、ここでの日付形式も異なります。ICU ユーザー ガイドを参照してください

于 2013-10-09T08:51:13.980 に答える
1

クイック フィルターを見つけましたreplace。コード スニペットは次のとおりです。

<div class="date"><span>{{ (create_time/1000)|date("m月d日,周D")|replace({'Mon':'一','Tue':'二','Wed':'三','Thu':'四','Fri':'五','Sat':'六','Sun':'日'}) }}</span></div>
于 2013-12-03T04:23:16.837 に答える
1

システムでsetlocale関数が機能しない場合は、このコードを試すことができます。

<?php
require_once dirname(__FILE__).'/vendor/autoload.php';

$loader = new Twig_Loader_String();
$twig   = new Twig_Environment($loader);

$twig->addFilter(new Twig_SimpleFilter('format_date', function($value) {
    $weekdays = array('日','一','二','三','四','五','六');

    return sprintf("%s, 周%s", date("m月d日"), $weekdays[date("w")]);
}));

echo $twig->render('{{ time_at | format_date }}', array(
    'time_at' => 1380813820000/1000
));
于 2013-10-09T13:17:48.913 に答える