Yii2查看日期时间格式(d-m-Y H:i:s)但是当DB更改格式保存/更新为Y-m-d时H:i:s
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yii2查看日期时间格式(d-m-Y H:i:s)但是当DB更改格式保存/更新为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:我:s(如24-09-2015 11:21:10)
但是当记录保存到数据库然后创建日期格式更改为
Y-m-d H:i:s(如2015-09-24 11:21:10)
如何在保存/更新记录时更改日期格式
答案
需要在控制器中保存/更新模型之前添加此代码。
喜欢,
// 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找到了答案。
在我的模型类中,我编写了行为代码:
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 frontendmodels;
use Yii;
use yiidbActiveRecord;
use yiiehaviorsAttributeBehavior;
/**
* 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)));
另一答案
以活动形式使用
'clientOptions'=> ['alias'=>'dd-mm-yyyy'],
以活动形式使用
echo MaskedInput::widget([
'name' => 'input-31',
'clientOptions' => ['alias' => 'date']]);
用班
类yii widgets MaskedInput
例
<?= $form->field($model, 'date_of_birth')->widget(yiiwidgetsMaskedInput::className(), [
'name' => 'input-31',
'clientOptions' => ['alias' => 'dd-mm-yyyy'], ]) ?>
另一答案
我有简单的代码,行为:
public function behaviors() {
return [
[ 'class' => yiiehaviorsTimestampBehavior::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' => yiiehaviorsTimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
],
// if you're using datetime instead of UNIX timestamp:
'value' => new Expression('NOW()'),
]
];
}
以上是关于Yii2查看日期时间格式(d-m-Y H:i:s)但是当DB更改格式保存/更新为Y-m-d时H:i:s的主要内容,如果未能解决你的问题,请参考以下文章
我正在尝试从 Swift 5 输入格式化的日期字符串为“yyyy-mm-dd H:i:s +0000”mySQL。相反,我得到“yyyy-mm-dd H:i:s 0000”。如何添加“+”?