B
B
Bombadile2016-06-30 08:33:40
Yii
Bombadile, 2016-06-30 08:33:40

How to display the date in a specific format depending on the locale in Yii2 (Russian - dd.mm.yyyy and Eng. - mm.dd.yyyy)?

It is required to display the date (for example, April 10, 2006) in a specific format specified in the config. Russian - 10.04.2006 and English 4.10.2006.
In the config, you can set predefined templates for the formatter such as short, medium, long, full. Which format the date like this:
- short: 10.04.06 and 4/10/06;
- medium: 10 Apr. 2006 and Apr 10, 2006;
- full: Monday, April 10, 2006 and Monday, April 10, 2006
- long: April 10, 2006 and April 10, 2006
How do I set the formats to 4/10/2006 and 10/4/2006?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Arutyunov, 2016-06-30
@arutyunov

If you didn’t find such a setting in the API and Formatter settings, then I believe that you can directly do something like this in the code:

if($lang == 'ru') {
  Yii::$app->formatter->asDate('php:d.m.Y');
} else if($lang == 'en') {
  Yii::$app->formatter->asDate('php:m.d.Y');
}

The code is greatly simplified, but I conveyed the idea.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question