How to display two date on kartik detail view like 01/01/2022-31/12/2022

76 Views Asked by At

I have two column in one table expiry_date and new_expiry_date. Now what i want i want to display date on detail view something like below, enter image description here

Here I have two model class one model class is for searching, Below is My offer Model class in which i have added my new_expiry_date and expiry_date .My table name is Offer table and i have defined my above both column inside offer table with data type date.

<?php

namespace backend\models;

use Yii;
use backend\models\Backendcommon;
class offer extends Backendcommon
{
    public $isEmail;
    public $transaction_id;
    public $recieve_calls;
    public $new_expiry_date;
    public $expiry_date;

    public static function tableName()
    {
        return 'offer';
    }
    public function rules()
    {
        return [
    
            [['isEmail','recieve_calls','transaction_id','new_expiry_date','expiry_date',], 'safe'],
            [['transaction_id'],'required'],
        ];
    }

}

My Controller class is defined below:-

<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\Backendcommon as Backendcommon;

class MeetmelicenceController extends BackendController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
 public function actionIndex($redirect = '')
    {
        
        $searchModel = new OfferSearch();
        $model       = new Offer();
        
        if(isset($_GET['OfferSearch'])) {
            $transaction_date = !empty($_GET['OfferSearch']['transaction_date']) ? date('Y-m-d',strtotime($_GET['OfferSearch']['transaction_date'])) : '';
            $new_expiry_date = !empty($_GET['OfferSearch']['new_expiry_date']) ? date('Y-m-d',strtotime($_GET['OfferSearch']['new_expiry_date'])) : '';
            $expiry_date = !empty($_GET['OfferSearch']['expiry_date']) ? date('Y-m-d',strtotime($_GET['OfferSearch']['expiry_date'])) : '';
            $_GET['OfferSearch']['new_expiry_date'] = $new_expiry_date;
            $_GET['OfferSearch']['expiry_date'] = $expiry_date;
            $_GET['OfferSearch']['transaction_date'] = $transaction_date;
            // echo $new_expiry_date;
            // exit;
            $searchModel->attributes = $_GET['OfferSearch'];  
        } 
            
        return $this->render('index', [
            'searchModel'  => $searchModel,
            'model'        => $model,
        ]);
    }
}

my View File is defined below:

<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\widgets\ActiveForm;
use common\widgets\Alert;
use kartik\widgets\DateTimePicker;
use yii\helpers\Url;
use yii\widgets\DetailView;

$this->title = Yii::t('app', "Manage Offer");
$this->params['breadcrumbs'][] = $this->title;
$createdflag = ($model->isNewRecord) ? true : false;

?>
    <div class="panel panel-default">
        <div class="panel-body" style="padding:0px">
        <?php // Alert::widget() ?>
        
        <!---  Filter ------>
        
        <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
                          [
                    'attribute' => 'expiry_date',
                    'headerOptions' => ['style' => 'width:10%'],
                    'label' => 'Licence Validity',
                    'filterInputOptions' => [
                        'class'       => 'form-control',
                        'placeholder' => 'Search'
                    ],
                    'value' => function($data)
                    {
                        $commonFun = new Functions;
                        // return $data['expiry_date'];
                        $expiry_date = '-';
                              if($data['expiry_date']!=null && $data['expiry_date']!='0000-00-00 ')
                              {
                                  $expiry_date = $commonFun->convertToUTCTimeZone($data['expiry_date'],'UTC','Asia/Singapore','d-M-Y');
                              }
                              return $expiry_date;
                        
                    },
                ],
                  ],
        ]) ?>        
        </div>
    </div>
                
1

There are 1 best solutions below

0
Insane Skull On

You are using model as $dataso you can access all attributes of model

'value' => function($data) {
      $commonFun = new Functions;
      $expiry_date = $new_expiry_date = '';
      if($data['expiry_date']!=null && $data['expiry_date']!='0000-00-00 ') {
           $expiry_date = $commonFun->convertToUTCTimeZone($data['expiry_date'],'UTC','Asia/Singapore','d-M-Y');
      }
      if($data['new_expiry_date']!=null && $data['new_expiry_date']!='0000-00-00 ') {
           $new_expiry_date = $commonFun->convertToUTCTimeZone($data['new_expiry_date'],'UTC','Asia/Singapore','d-M-Y');
      }
      return $expiry_date .' - '. $new_expiry_date ;
},