日付形式を次のように表示したい10月09日, 周三
(つまり、10/9、水曜日を意味します)。データは [message.time]: の
よう
な UNIX 時間
です。 w" は数字であり、中国語のテキストではありません。
テキスト形式を変換するためにTwigで何かできるでしょうか?1380813820000
{{ (message.time/1000)|date("m月d日, 周w") }}
10月09日,周3
ありがとう
根本的な問題は、Twig がDateTime::format
ロケールや (私の知る限り) 曜日の名前を変換する他のタイプの機能をサポートしないメソッドを使用することです。
次の 3 つの解決策があります。
strftime
ロケール (およびローカライズされた曜日名) をサポートする を使用します。intl
PHPの拡張機能を使用できる場合は、Twig の拡張機能が付属しているTwig-extensionsintl
を使用できます。さらに、Twig テンプレートで好みのソリューションを使用するには、Twig の機能を拡張する必要があります。
次の (かなり大きな) コードは、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
;-))。
もちろん、ロケールには、おそらく注意が必要な制限のリストがあります。
setlocale
動作が異なり、結果が異なります)。のPHPマニュアルをチェックしてくださいsetlocale
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 ユーザー ガイドを参照してください。
クイック フィルターを見つけましたreplace
。コード スニペットは次のとおりです。
<div class="date"><span>{{ (create_time/1000)|date("m月d日,周D")|replace({'Mon':'一','Tue':'二','Wed':'三','Thu':'四','Fri':'五','Sat':'六','Sun':'日'}) }}</span></div>
システムで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
));