Search code examples
arraysdateyii2formathelper

Yii2 ArrayHelper::map() format date


I have following in index.php/gridview widget:

'filter' => ArrayHelper::map(Hwzrpp::find()->all(), 'id', 'wa'),

wa is a date stored in DB in the format: 2018-12-12. Is there a quick way to apply another format with e.g. :date or Yii::$app->formatter->asDate() somewhere in map() to it, so that I can show date like this: 12.12.2018 in the dropdown? I don't find anything regarding this.


Solution

  • You can pass Closure as third argument for map():

    'filter' => ArrayHelper::map(Hwzrpp::find()->all(), 'id', function ($data) {
        return Yii::$app->formatter->asDate($data->wa);
    }),