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:
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():
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.
If the
datetime
field is to be set automatically you could change it to atimestamp
instead and setCURRENT_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.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
if you only want to modify your view try the -> kartiks TimePicker widget
ex:
in user or registration controller just write inside actionCreate() method..
it will automatically insert your user created time.