Combining Fields (e.g. Date + Time => DATETIME) ?

I’ve got a table with a DATETIME field in it. I wondered what the preffered way was for combining a seperate date field (as a datepicker) and a time field on a form to put into the DATETIME field of the database?

I originally had two separate fields in the database but this messes up other parts of the app. I tried having 2 form fields on the page but they need linking to fields in the database so that doesn’t work either.

Any help would be hugely appreciated. :)

I had exactly the same problem, with only difference that I had a date column in my DB and wanted two fields for start date and end date to be able to display records which date is inside defined date range. With a help of mdomba I was able to do this quickly and painlessly. I’ll give you some advices on steps you should take to solve this problem, but you must try to implement it yourself over a weekend. If you want to get a complete, working example, then you have wait until Monday as I have it at my work and I don’t have access to those files from home! :]

Here is what you have to take care of:

Step 1: In your model definition file declare two new public variables (model attributes), for example $date and $time.

Step 2: Make sure, you add these variable names (without dollar sign) into your rules() method to (usually) last line, which ends with ‘safe’, ‘on’=>‘search’ and where you declare model attributes (variables) that can be search, but this also means that these variables are safe for massive assignment, which is what we want here. Because this will make Yii to send values entered by your user to your form, to these new fields along with other model attributes.

Step 3: Edit your form file to add two new input box, one as a datepicker and second for a time.

Step 4: Edit your view or controller, where you process input from the form (or search() method in your model file, if you use it for example for searching CGridView). Here, in place of $model->DATETIME which you used to access DATETIME field in your DB, you can now use $model->date and $model->time and do whatever you want with them.

The simplest solution would be to combine them together (with using PHP dot operator - .) and use in search. So, if originally in your search() method in your model you used:

$criteria->compare('datetime', $this->datetime);

to compare DATETIME field in DB with value from datetime field in the form, you can now use something like that:

$criteria->compare('datetime', $this->date.$this->time);

to compare DATETIME filed with combined result from fields date and time in your form.

Write, if you have any further questions.

Hello Ashiro,

I would advice against using two different fields for this and instead do the user interface with jquery. What I mean is that the original date field would be hidden by jquery and would be update it when the fields generated by jquery are changed by e.g. picking a date with datepicker.

I also separated the field in one of my projects exactly like Tjeder described above, but I thought it just made things unnecessary complicated so I changed the user interface to be handled entirely with jquery. This way the date/time picking will be fancy when javascript in enabled and otherwise a simple textfield will be displayed.

If you think about it you will still need to require javascript to be enabled for datepicker and other plugins/widgets.

Hope this helps.