Yii::$app->formatter->asDate() output is not correct

In my view file I have:

echo Yii::$app->formatter->asDate($el['date'],'php:D d-M-Y' . ' ' . Yii::$app->getTimeZone());

and the value of the date is :

but I am getting from the formatter:

On Sun 26-Jun-2016 PM06UTCSun, 26 Jun 2016 13:24:10 +0000242016-06-26T13:24:10+00:00PM/SunUTC6GMTUTCSun,

26 Jun 2016 13:24:10 +0000

instead of


I don’t get what’s wrong.

You should use:

echo Yii::$app->formatter->asDate($el['date'],'php:D d-M-Y');

and set your timezone in your config file. You should the default date format in your config too.

Additionally, if you have users from different timezones then you can change the timezone dynamically using the event on beforeRequest:

'on beforeRequest' => function () {        

     // Get the zone

     $timeZone = 'America/Los_Angeles';



Check out available timezones:


For your first proposition: I add that timezone so that the formatter doesn’t convert the date to the application timezone, I am using the formatter just to add the name of the day(sunday…) and month(june…) in the application language(en,fr,ru,hi,es,…), It does the translate well. If there is another way that can support different languages, I can use it.

For the second proposition: I use an extension, here is the configuration in component:

      'timezone' => [

        'class' => 'yii2mod\timezone\Timezone',

        'actionRoute' => '/site/timezone' //optional param - full path to page must be specified


and bootstrap:

‘bootstrap’ => [‘log’,‘timezone’,


so it does it. The only on problem is those nonsense words that it is add, if I remove the Yii::$app->getTimezone(), it’s working well(but the problem is, it is converte the parameter date I passs to the app timezone.

Timezone should be add to the date directly not format :

echo Yii::$app->formatter->asDate($el[‘date’] . ’ ’ .Yii::$app->getTimeZone() ,‘php:D d-M-Y’);


echo Yii::$app->formatter->asDate($el[‘date’],'php:D d-M-Y ’ .Yii::$app->getTimeZone() );

You don’t need to use external extensions when Yii has its native way.

You just need to add your default time zone:

  'timeZone' => 'Europe/London',

Here’s another example: http://www.yiiframework.com/doc-2.0/guide-structure-applications.html#timeZone

But sometimes you cannot set a default time zone because you have users all around the world. Because of that, as I have earlier said, you have to change a time zone dynamically.

You can add the following piece of code:

 'on beforeRequest' => function () {


       $time_zone = Yii::$app->user->identity['time_zone'];




       } else {





You just need to have a “time_zone” column on the table where you store users. It’s usually called “user” or “users”.

That’s it. I use this piece of code on my every app. You just need to save all dates and times in UTC format.

Here are more examples about date formatting:


Good luck with the app.