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

问题描述 投票:7回答:4

我正在使用Kartik DateTimePicker扩展

<?= $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:我:s(如24-09-2015 11:21:10)

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

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

如何在保存/更新记录时更改日期格式

yii2 yii2-advanced-app
4个回答
3
投票

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

喜欢,

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

要么

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


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)));

0
投票

以活动形式使用

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

以活动形式使用

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

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()'),
       ]
    ];
}
© www.soinside.com 2019 - 2024. All rights reserved.