I want to show the date format like: 10月09日, 周三
(which means 10/9, Wed.)
and my data is UNIX time like [message.time]: 1380813820000
so this is what I do in twig:
{{ (message.time/1000)|date("m月d日, 周w") }}
But it show me: 10月09日,周3
, Because date "w" are numbers, not Chinese text.
so can I do anything by Twig to converse the text format?
thanks
I found a quick filter
replace
, here is the code snippet:The underlying problem is, that Twig uses the
DateTime::format
method, which does not support locales or (as far as I know) any other type of functionality to translate the names of the weekdays.There are three solutions:
strftime
, which supports locales (and thus localized weekday-names).intl
extension of PHP, then you can use the Twig-extensions which comes with aintl
extension for Twig.Plus to use your preferred solution in a Twig-template, you have to extend the functionality of Twig.
Using strftime and setlocale
The following (rather large) code implements the
strftime
solution:This code shows
10月09日, 周三
on my system (after I installed the debian packagelocales-all
;-) ).Of course, locales come with a list of restrictions you probably need to be aware of:
setlocale
works different/gives different results). Check out the PHP-manual ofsetlocale
Using intl and the Twig-extensions
If you can use the
intl
extensions and the "Twig-extensions"-package, you have to uselocalizeddate
instead ofdate
:That code also shows
10月09日, 周三
- it even adds the 周-thingie automatically.Of course, the date-format here is also different - check out the ICU user guide.
You can try this code if the setlocale function cannot working on you system.