Yii2 View DateTime Format (d-m-Y H:i:s) But When Save/update in DB 改变格式为Y-m-d H:i:s
Yii2 View DateTime Format (d-m-Y H:i:s) But When Save/update in DB Change format to Y-m-d H:i:s
我正在使用 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:i:s(如 24-09-2015 11:21:10)
但是当记录保存到数据库时,创建日期格式更改为
Y-m-d H:i:s(如 2015-09-24 11:21:10)
如何更改记录save/update 上的日期格式
只需在控制器中的 save/update 模型之前添加此代码。
喜欢,
// 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
最后我使用 AttributeBehavior 找到了答案。
在我的模型中 class 我写了 行为代码 :
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));
},
],
];
}
我的模型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',
];
}
}
如果输入格式是d/m/Y那么你需要将"/"替换成"-"
喜欢: 输入日期(创建):10/09/2015
date('Y-m-d H:i:s', strtotime(str_replace("/","-",$this->created)));
以活跃的形式使用
'clientOptions' => ['alias' => 'dd-mm-yyyy'],
以活跃的形式使用
echo MaskedInput::widget([
'name' => 'input-31',
'clientOptions' => ['alias' => 'date']]);
使用class
Class yii\widgets\MaskedInput
例子
<?= $form->field($model, 'date_of_birth')->widget(\yii\widgets\MaskedInput::className(), [
'name' => 'input-31',
'clientOptions' => ['alias' => 'dd-mm-yyyy'], ]) ?>
我有简单的代码,
在行为上:
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()'),
]
];
}
public function behaviors() {
return [
[
'class' => BlameableBehavior::className(),
],
[
'class' => TimestampBehavior::className(),
'value' => date('Y-m-d H:i:s')
],
];
}
我正在使用 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:i:s(如 24-09-2015 11:21:10)
但是当记录保存到数据库时,创建日期格式更改为
Y-m-d H:i:s(如 2015-09-24 11:21:10)
如何更改记录save/update 上的日期格式
只需在控制器中的 save/update 模型之前添加此代码。
喜欢,
// 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
最后我使用 AttributeBehavior 找到了答案。
在我的模型中 class 我写了 行为代码 :
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));
},
],
];
}
我的模型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',
];
}
}
如果输入格式是d/m/Y那么你需要将"/"替换成"-"
喜欢: 输入日期(创建):10/09/2015
date('Y-m-d H:i:s', strtotime(str_replace("/","-",$this->created)));
以活跃的形式使用
'clientOptions' => ['alias' => 'dd-mm-yyyy'],
以活跃的形式使用
echo MaskedInput::widget([
'name' => 'input-31',
'clientOptions' => ['alias' => 'date']]);
使用class
Class yii\widgets\MaskedInput
例子
<?= $form->field($model, 'date_of_birth')->widget(\yii\widgets\MaskedInput::className(), [
'name' => 'input-31',
'clientOptions' => ['alias' => 'dd-mm-yyyy'], ]) ?>
我有简单的代码, 在行为上:
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()'),
]
];
}
public function behaviors() {
return [
[
'class' => BlameableBehavior::className(),
],
[
'class' => TimestampBehavior::className(),
'value' => date('Y-m-d H:i:s')
],
];
}