2015-09-24 6 views
7

Używam Kartik DateTimePicker ExtensionYii2 Zobacz DateTime Format (DMY H: i: s), ale kiedy zapisać/UPDATE DB Change formacie Ymd H: i: s

<?= $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 
     ] 
    ]) 
?> 

użytkownika wypełnić Data utworzenia format jest

DMY H: i: s (jak 24-09-2015 11:21:10)

Ale kiedy rekord Zapisz w bazie następnie utworzyć format daty zmiany

YMD H: i: s (jak 2015-09-24 11:21:10)

Jak mogę zmienić format daty przy zapisywaniu/aktualizację rekordu

+0

Jeśli tylko zmienić kolumnę 'Utworzony' do 'created_at', kod będzie bardziej czysty. –

Odpowiedz

2

Wreszcie znalazłem odpowiedź korzystając AttributeBehavior.

W mojej klasie modelu mam napisać kod zachowań:

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)); 
      }, 
     ], 
    ]; 
} 

moim modelu klasy

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', 
     ]; 
    } 
} 

Jeśli format wejściowy jest D/M/Y wtedy należy zastąpić "/" przez "-"

jak: data wejścia (utworzone) 10/09/2015

date('Y-m-d H:i:s', strtotime(str_replace("/","-",$this->created))); 
3

trzeba po prostu dodać ten kod przed zapisz/zaktualizuj model w kontrolerze.

jak,

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

LUB

// 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 

Więcej informacji można znaleźć ten link

+0

Czy jest jakaś inna metoda zmiany formatu daty? jak beforeSave() – Kailas

+0

moje dane wejściowe to datetime nie data – Kailas

+0

Dzięki @gamitg.Rozwiązanie znalezione przy użyciu zachowań – Kailas

0

zastosowania w czynnej postaci

'clientOptions' => [ 'ps' => 'dd mm rrrr' ]

zastosowania w czynnej postaci

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

klasy zastosowanie

Class widgety Yii \ \ MaskedInput

Przykład

<?= $form->field($model, 'date_of_birth')->widget(\yii\widgets\MaskedInput::className(), [ 
    'name' => 'input-31', 
    'clientOptions' => ['alias' => 'dd-mm-yyyy'],  ]) ?> 
Powiązane problemy