INT to Date in View

Afternoon,

SO…

I save datetime in my Mysql as an Int (created_at) and would like to convert it to date format in my view.


<div class="quote-index">


    <?= GridView::widget([

    	'dataProvider' => $dataProvider,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'description',

            'total_cost',

            'created_at',

            

            ['class' => 'yii\grid\ActionColumn', 'template' => '{view}'],

        ],

    ]); ?>

</div>

is there a build in function for this?

Cheers

Try with:




        'columns' => [

           ...

            [

                'attribute' => 'created_at',

                'format' => ['date', 'php:d/m/Y H:i'],

            ],  

         ],



or




        'columns' => [

           ...

            [

                'attribute' => 'created_at',

                'value' => function($model) {

                       return date('d/m/Y', $model->created_at);

                }

            ],  

         ],



Perfect Thank You!

Is there a way to put this in the model so it always has the same formatting?

Thank you for your help!

Sure!

In the model




public function getCreatedAtText()

{

     return date('d/m/Y H:i', $this->created_at);

}



In the grid view




[ 'attribute' => 'createdAtText' ]