8

I'm Using Kartik DateTimePicker Extension

<?= $form->field($model, 'Created')->widget(DateTimePicker::classname(),[
        'model' => $model,
        'attribute' => 'Created',
        'name' => 'Created',
        'options' => ['placeholder' => 'Select Created'],
        'pluginOptions' => [
            'format' => 'dd-mm-yyyy hh:ii:ss',
            'todayHighlight' => true
        ]
    ]) 
?>

User fill the Create Date the format is

d-m-Y H:i:s (like 24-09-2015 11:21:10)

But when record save to database then Create Date Format change to

Y-m-d H:i:s (like 2015-09-24 11:21:10)

How can I change the date format on save/update of record

Kailas
  • 3,173
  • 5
  • 42
  • 52

5 Answers5

3

Need to just add this code before save/update model in controller.

like,

// ICU format
$model->Created = Yii::$app->formatter->asDate($_POST['modelName']['Created'], 'yyyy-MM-dd HH:mm:ss'); // 2014-10-06 15:22:34

OR

// PHP date()-format
$model->Created = Yii::$app->formatter->asDate($_POST['modelName']['Created'], 'php:Y-m-d H:i:s'); // 2014-10-06 15:22:34

For more information refer this link

GAMITG
  • 3,810
  • 7
  • 32
  • 51
2

Finally I found the answer using AttributeBehavior.

In my model class I've write the behaviors code:

public function behaviors()
{
    return [
        [
            'class' => AttributeBehavior::className(),
            'attributes' => [
                // update 1 attribute 'created' OR multiple attribute ['created','updated']
                ActiveRecord::EVENT_BEFORE_INSERT => ['created','updated'],
                ActiveRecord::EVENT_BEFORE_UPDATE => 'updated',
            ],
            'value' => function ($event) {
                return date('Y-m-d H:i:s', strtotime($this->Created));
            },
        ],
    ];
}

My Model Class

    namespace frontend\models;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\AttributeBehavior;
/**
 * This is the model class for table "product".
 *
 * @property integer $id
 * @property integer $product_id
 * @property string $product_name
 * @property string $created
 * @property string $updated
 */
class Product extends ActiveRecord
{
    public $csv_file;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'product';
    }

    public function behaviors()
    {
        return [
            [
                'class' => AttributeBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created','updated'], // update 1 attribute 'created' OR multiple attribute ['created','updated']
                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated', // update 1 attribute 'created' OR multiple attribute ['created','updated']
                ],
                'value' => function ($event) {
                    return date('Y-m-d H:i:s', strtotime($this->LastUpdated));
                },
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'product_id', 'product_name', created, updated], 'required'],

        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'product_id' => 'Product ID',
            'product_name' => 'Product Name',
            'created' => 'Created',
            'updated' => 'Updated',
        ];
    }
}

If input format is d/m/Y then you need to replace the "/" by "-"

like: input date(created): 10/09/2015

date('Y-m-d H:i:s', strtotime(str_replace("/","-",$this->created)));
Kailas
  • 3,173
  • 5
  • 42
  • 52
0

use in active form

'clientOptions' => ['alias' => 'dd-mm-yyyy'],

use in active form

echo MaskedInput::widget([
'name' => 'input-31',
'clientOptions' => ['alias' =>  'date']]);

use class

Class yii\widgets\MaskedInput

Example

<?= $form->field($model, 'date_of_birth')->widget(\yii\widgets\MaskedInput::className(), [
    'name' => 'input-31',
    'clientOptions' => ['alias' =>  'dd-mm-yyyy'],        ]) ?>   
Andy
  • 49,085
  • 60
  • 166
  • 233
user7641341
  • 169
  • 1
  • 5
0

I have simple code, in behavior:

 public function behaviors() {
    return [
       [ 'class' => \yii\behaviors\TimestampBehavior::className(),
            'attributes' => [
                ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
            ],
            // if you're using datetime instead of UNIX timestamp:
             'value' => new Expression('NOW()'),
       ]
    ];
}

for rules:

 public function behaviors() {
    return [
       [ 'class' => \yii\behaviors\TimestampBehavior::className(),
            'attributes' => [
                ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
            ],
            // if you're using datetime instead of UNIX timestamp:
             'value' => new Expression('NOW()'),
       ]
    ];
}
Dedy Kurniawan
  • 286
  • 2
  • 3
  • 9
0
 public function behaviors() {
        return [
            [
                'class' => BlameableBehavior::className(),
            ],
            [
                'class' => TimestampBehavior::className(),
                'value' => date('Y-m-d H:i:s')
            ],
        ];
    }
  • Could you please explain *why* and *how* your code snippet provides an answer to the question? Thanks. – deHaar Aug 02 '19 at 06:42