Yii2 View DateTime Format (dmY H: i: s) But when Save / update in DB changes the format to Ymd H: i: s

I am using the 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 ] ]) ?> 

The user will fill in the creation date, format

dmY H: i: s (e.g. 24-09-2015 11:21:10)

But when the record is saved to the database, then change the date format to

Ymd H: i: s (e.g. 2015-09-24 11:21:10)

How to change the date format when saving / updating a record

+7
yii2 yii2-advanced-app
source share
4 answers

Finally, I found the answer using AttributeBehavior .

In my model class, I wrote a behavior 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('Ymd 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('Ymd 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 the input format is d / m / Y , you need to replace "/" with "-"

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

 date('Ymd H:i:s', strtotime(str_replace("/","-",$this->created))); 
+2
source share

You just need to add this code before saving / updating the model in the controller.

as

 // 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:Ymd H:i:s'); // 2014-10-06 15:22:34 

For more information see the link

+3
source share

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'], ]) ?> 
0
source share

I have simple code in the 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()'), ] ]; } 
0
source share

All Articles