Search code examples
phpdatetimecakephpform-helpers

CakePHP Form Helper and Datetime


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?


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.