Datetime Picker Questions

Hello All,

I need some help with what I am sure is a simple solution toa problem that I am spending to much time trying to resolve. Let me setup theproblem and please remember that I am very new to YII and PHP.

I followed the Blog Tutorial and setup my MySQL database asrequired and all works well. I can add, update and deleted without problems.

Now I wanted the user to be able to change the date for create_time,so I added the jquery datetime picker. This is where I have the problem. Thecreate_time field is an INT (11), which is setup for a Unix Timestamp. Thedatetimepicker has an option for Unix timestamps, dateFormat => ‘@’, butthis is adding millisecond’s to the date.

So for October 23, 2023 the timestamp is 1382547600, whichthe date picker can’t read and if I select that date from the date picker itputs 1382547600000 in the field, but it will not save it. The SQL error that Iam getting is the field is to small so I increased the field to 15 and triedagain with no luck the same SQL error.

Researching the problem tells me that I need to divide thestamp by 1000. When I select the date and remove 3 zeros the date shows upcorrectly, but again the date picker will not pick it up correctly.

I am not sure where I should be dividing and multiplying toget the correct dates, I am guessing it will be in the post controller.

My goal id to use the altFormat => ‘@’ and thendateFormat to display the format I want to see.

I hope someone can give me some insight or guidance to helpme resolve this issue. Thank you in advance for your time.

Normally I’d put data transformations into the model, but in this case I don’t think it belongs there. You’re dealing with a mismatch between the representation in the model and the view, so perhaps the transformation belongs in the controller.

Something like this might work in the action:




    public function actionEdit($id)

    {

        $model = YourArModel::model()->findByPk($id);


        if (isset($_POST['YourArModel']))

        {

            $model->attributes = $_POST['YourArModel'];


            if (isset($_POST['YourArModel']['create_time']))

                $model->create_time = (int)($_POST['YourArModel']['create_time'] / 1000);


            if ($model->save())

            {

                // Set a flash message here if you like

                $this->refresh();

            }

        }


        $model->create_time *= 1000;

        $this->render('edit', array('model'=>$model));

    }



While it might work, it seems wrong to perform what looks like an arbitrary conversion in the controller.

Another option is to have an extra field in the model called something like create_time_ms and set your validation rules against that field. You could then use beforeSave() and afterFind() to populate the create_time and create_time_ms fields respectively. This also seems wrong though.