使用 Yii 自动存储日期时间

2022-01-04 00:00:00 datetime php yii

我开始使用 Yii 学习 php,我有一个很大的问题,也许你可以帮助我.

我正在使用表单来创建用户.我想将我在表单中引入的用户的数据存储到数据库(MySQL)中,但我还必须将日期和时间存储在数据库的日期时间字段中.我看到了一些像 CJUIDATEPICKER 这样的扩展来选择日期,但我不知道任何扩展来选择日期时间.我认为更好的方法是在创建新用户时自动存储日期时间.现在我有一个表单中的文本框,我必须在其中引入格式为 yyyy-mmmm-dddd hh:mm:ss 的日期时间,但我认为它没有用.

拜托,有人可以帮助我并向我解释如何在我在数据库中创建寄存器时存储日期时间?

非常感谢.

现在我使用 "$model->reg_date=new CDbExpression('NOW()');"在 actionCreate() 控制器内部,它运行良好,但我认为最好在编写任何内容之前在用户创建表单的字段中显示此日期时间,并在单击提交时存储表单的所有字段.我尝试这样做,但正如您可以想象的那样,该字段仅显示NOW()"

这是我在 _form.php 中的表单代码的一部分:

<?php $model->reg_date=new CDbExpression('NOW()');?><?php echo $form->labelEx($model,'reg_date');?><?php echo $form->textField($model,'reg_date');?><?php echo $form->error($model,'reg_date');?>

有人可以帮助我吗?

再次感谢您!

解决方案

如果您选择在创建新用户时自动存储日期时间,我会使用 CDbExpression:

$model->created = new CDbExpression('NOW()');//"YYYY-MM-DD HH:MM:SS"

和 UNIX_TIMESTAMP(NOW()) 用于 Unix 时间戳.值得一提的是,这些函数是特定于 MySQL 的.

我还想指出 CJuiDatePicker 是一个 widget 与 Yii 捆绑在一起.抱歉挑剔了.

<小时>

使用 php 的日期函数而不是 MySQL 的 NOW() 更新答案:

//UserController.php公共函数 actionCreate(){$model=新用户;$model->created = date("Y-m-d H:i");//添加了这个.if(isset($_POST['用户'])){$model->attributes=$_POST['U​​ser'];if($model->save()){$this->redirect(array('view','id'=>$model->id));}}$this->render('create',array('模型'=>$模型,));}//_form.php 在您的活动表单中echo $form->labelEx($model,'created');//以任何你想要的方式显示它.也许是标签?echo $form->hiddenField($model, 'created');//在提交时将值发送回控制器

应该是这样!

如果您使用引导程序,还有一些值得一提的地方.不要在表单中首先放置隐藏字段,因为引导程序的第一个子 css 选择器会弄乱顶部边距.

I am starting to learn php using Yii, and I have a big question, maybe you can help me.

I am using a form to create users. I want to store the data of the users that I introduce in the form into the DB (MySQL), but I also must store the date and the time in the datetime field of the DB. I saw some extensions like CJUIDATEPICKER to pick the date, but I don't know any extension to pick the datetime. I think that the better way is to store the datetime automatically when I create a new user. Now I have a textbox in the form where I have to introduce the datetime with the format yyyy-mmmm-dddd hh:mm:ss, but I think that it isn't useful.

Please, someone coud help me and explain to me how to store a datetime when I create a register into the DB?

Thank you very much.

EDIT:

Now I use "$model->reg_date=new CDbExpression('NOW()');" inside the actionCreate() controller and it works well but I think that would be better to show this datetime inside the field of the user creation form before you write anything and to store all the fields of the form when you click on submit. I tried to do it but as you can imagine the field only show "NOW()"

This is part of my code of the form in _form.php:

<div class="row">
<?php $model->reg_date=new CDbExpression('NOW()');?>
<?php echo $form->labelEx($model,'reg_date'); ?>
<?php echo $form->textField($model,'reg_date'); ?>
<?php echo $form->error($model,'reg_date'); ?>
</div>

Someone could help me?

Thank you again!

解决方案

If you choose to store the datetime automatically when you create a new user, I'd use CDbExpression:

$model->created = new CDbExpression('NOW()'); // "YYYY-MM-DD HH:MM:SS"

And UNIX_TIMESTAMP(NOW()) for a unix timestamp. It should be worth mentioning that those functions are MySQL-specific.

I'd also like to point out that CJuiDatePicker is a widget that comes bundled with Yii. Sorry for the nitpicking.


Updated answer using php's date function instead of MySQL's NOW():

// UserController.php
public function actionCreate()
{
    $model=new User;
    $model->created = date("Y-m-d H:i");  // Added this.
    if(isset($_POST['User']))
    {
        $model->attributes=$_POST['User'];
        if($model->save())
        {
            $this->redirect(array('view','id'=>$model->id));
        }
    }
    $this->render('create',array(
        'model'=>$model,
    ));
}

// _form.php  inside your Active Form
echo $form->labelEx($model,'created');   // Display it anyway you'd like. A label perhaps?
echo $form->hiddenField($model, 'created'); // Send back value to the controller on submit

That should be it!

And something worth mentioning if you use bootstrap. Don't place a hidden field first in a form because bootstraps first-child css-selectors will mess up the top margin.

相关文章