Using the yii2-datecontrol extension along with yii2-detail-view widget

You are viewing revision #7 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.

« previous (#6)

  1. Scenario
  2. yii2-datecontrol Module Settings
  3. yii2-detail-view Attribute Configuration

The yii2-datecontrol extension allows you to control separate date formats for display and saving for form inputs.

The yii2-detail-view extension, enhances the yii DetailView and enables you to toggle between VIEW and EDIT modes with other features.

This article explains you how you combine both. That is, how you can enable date formats for display and save using the DateControl extension within yii2-detail-view.

Note: To setup the controller and view to work with yii2-detail-view extension for update and delete, you can refer this wiki.


Let's assume you want to display dates as "d-m-Y" format and save dates as "Y-m-d" format to db.

yii2-datecontrol Module Settings

Setup your datecontrol module as follows:

'modules' => [
    'datecontrol' => [
        'class' => 'kartik\datecontrol\Module',

        // format settings for displaying each date attribute
        'displaySettings' => [
            'date' => 'd-M-Y',
            'time' => 'H:i:s A',
            'datetime' => 'd-m-Y H:i:s A',

        // format settings for saving each date attribute
        'saveSettings' => [
            'date' => 'Y-m-d', 
            'time' => 'H:i:s',
            'datetime' => 'Y-m-d H:i:s',

        // automatically use kartik\widgets for each of the above formats
        'autoWidget' => true,

yii2-detail-view Attribute Configuration

Setup your yii2-detail-view date related attributes to use the DateControl extension as mentioned below:

use kartik\detail\DetailView;
use kartik\datecontrol\DateControl;

echo DetailView::widget([
            'format'=>['date', 'd-m-Y'],
            'type'=>DetailView::INPUT_WIDGET, // enables you to use any widget

With the above settings, you should see the dates getting displayed in 'd-m-Y' format and saved in 'Y-m-d' format.