php - CakePHP Form Helper and Datetime

372

I have used the form helper to create a date time selection and when I access the$this->data.

It looks like the following.

[Timetable] => Array
    (
        [event_id] => 133
        [location_id] => 39
        [start] => Array
            (
                [hour] => 09
                [min] => 06
                [day] => 11
                [month] => 03
                [year] => 2011
            )

    )

But I want this to look more like this...

[Timetable] => Array
    (
        [event_id] => 133
        [location_id] => 39
        [start] => 2011-03-11 09:06:00

    )

Is there a way to transform it to this?

237

Answer

Solution:

You can just rebuild the$this->data['Timetable']['start'] var in your controller like so:

$this->data['Timetable']['start'] = $this->data['Timetable']['start']['year']
    .'-'.$this->data['Timetable']['start']['month']
    .'-'.$this->data['Timetable']['start']['day']
    .' '.$this->data['Timetable']['start']['hour']
    .':'.$this->data['Timetable']['start']['min'];

Should work fine.

82

Answer

Solution:

Since you're using the form helper to generate your code, you can't get it into a single index unless you change your form helper.

You're probably using

$this->Form->input('start')

Changing it to the below will make it come out the way you want.

$this->Form->input('start', array('type'=>'text'));

However, if you do this, you'll lose out on all the dropdowns that cake auto generates for you. Not a problem if you use a datepicker.

People are also looking for solutions to the problem: oop - Overloading construct in php?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.