How best to convert CakePHP date picker form data to a PHP DateTime object?

Posted by Daren Thomas on Stack Overflow See other posts from Stack Overflow or by Daren Thomas
Published on 2010-05-04T10:09:21Z Indexed on 2010/05/04 15:18 UTC
Read the original article Hit count: 419

Filed under:
|
|
|

I'm doing this in app/views/mymodel/add.ctp:

<?php echo $form->input('Mymodel.mydatefield'); ?>

And then, in app/controllers/mymodel_controller.php:

function add() {
    # ... (if we have some submitted data)
    $datestring = $this->data['Mymodel']['mydatefield']['year'] . '-' .
                  $this->data['Mymodel']['mydatefield']['month'] . '-' .
                  $this->data['Mymodel']['mydatefield']['day'];
    $mydatefield = DateTime::createFromFormat('Y-m-d', $datestring);
}

There absolutly has to be a better way to do this - I just haven't found the CakePHP way yet...

What I would like to do is:

function add() {
    # ... (if we have some submitted data)       
    $mydatefield = $this->data['Mymodel']['mydatefiled']; # obviously doesn't work
}

© Stack Overflow or respective owner

Related posts about cakephp

Related posts about php