Yii2查看日期时间格式(d-m-Y H:i:s)但是当DB更改格式保存/更新为Y-m-d时H:i:s

时间:2015-09-24 05:55:57

标签: yii2 yii2-advanced-app

我使用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
        ]
    ]) 
?>

用户填写格式为

的创建日期

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

但是当记录保存到数据库时,创建日期格式更改为

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

如何在保存/更新记录

时更改日期格式

5 个答案:

答案 0 :(得分:3)

需要在控制器中保存/更新模型之前添加此代码。

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

有关详细信息,请参阅此link

答案 1 :(得分:2)

最后,我使用 AttributeBehavior 找到答案。

在我的模型课程中,我编写了行为代码

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

我的模特课

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

如果输入格式为 d / m / Y ,则需要将“/”替换为“ - ”

赞:输入日期(已创建):2015年9月10日

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

答案 2 :(得分:0)

以活动形式使用

&#39; clientOptions&#39; =&GT; [&#39;别名&#39; =&GT; &#39; DD-MM-YYYY&#39],

以活动形式使用

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

使用课程

类yii \ widgets \ MaskedInput

实施例

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

答案 3 :(得分:0)

我有简单的代码, 行为:

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

规则:

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

答案 4 :(得分:0)

 public function behaviors() {
        return [
            [
                'class' => BlameableBehavior::className(),
            ],
            [
                'class' => TimestampBehavior::className(),
                'value' => date('Y-m-d H:i:s')
            ],
        ];
    }