to store datetime automatically using Yii

2019-04-10 19:13发布

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!

标签: php datetime yii
5条回答
我欲成王,谁敢阻挡
2楼-- · 2019-04-10 19:39

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.

查看更多
该账号已被封号
3楼-- · 2019-04-10 19:42

If the datetime field is to be set automatically you could change it to a timestamp instead and set CURRENT_TIMESTAMP as its default as shown in this answer.

If you insist on using datetime, you could use a trigger as explained in this answer.

查看更多
成全新的幸福
4楼-- · 2019-04-10 19:51

No Need to Add date time feild in your form *Just remove the date time feild from your Form*.

And Add CDbExpression('NOW()') to your Model in rules, Just see Example

public function rules()
    {
        return array(
//date_modified and date_created is my database field names..
               array('date_modified','default',
                    'value'=>new CDbExpression('NOW()'),//automatically add the current date in mysql feild
                     'setOnEmpty'=>false,'on'=>'update'),
                   array('date_created,date_modified','default',
                'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'insert'),
);
}
查看更多
Deceive 欺骗
5楼-- · 2019-04-10 20:03

if you only want to modify your view try the -> kartiks TimePicker widget

ex:

<?= $form->field($model, 'time')->widget(TimePicker::classname(),[
'pluginOptions' => ['defaultTime' => 'current'] ]);?>
查看更多
趁早两清
6楼-- · 2019-04-10 20:06

in user or registration controller just write inside actionCreate() method..

public function actionCreate()
{
       if(isset($_POST['user']
       {
             $model->attributes=$_POST['user'];
             $model->created_date=new CDbExpression('NOW()'); //inserting created time

             // ...
       }
}

it will automatically insert your user created time.

查看更多
登录 后发表回答