0

I need to convert an integer number to date using the Yii2 Framework for PHP.

I can convert it using the PHP function like this (as I found in this question):

date("Y-m-d H:i:s", 1498675028);

But I am new using Yii2 so I am not sure how to do it and maybe it can be done with Yii2.

I have a table with some columns and two of them (created_at and updated_at) has the integer 1498675028 (it is in the Unix time stamp format). But the user see in the view the integer number instead of a date. This is part of my index.php view file:

<?php Pjax::begin(); ?>    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
            'username',
            'created_at',
            'updated_at',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
<?php Pjax::end(); ?>
Roby Sottini
  • 2,117
  • 6
  • 48
  • 88

1 Answers1

3
  <?php Pjax::begin(); ?> 

    <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'id',
        'username',
        'created_at:date',
        'updated_at:date',
        [
          'attribute' => 'updated_at',
           'value' => function ($model, $key, $index, $grid) {
                      return date('Y-m-d', $model->updated_at);
            },
       ], 
        ['class' => 'yii\grid\ActionColumn'],
    ],
    ]); ?>
<?php Pjax::end(); ?>
Dharmendra Singh
  • 1,186
  • 12
  • 22