如何最好将CakePHP日期选择器表单数据转换为PHP DateTime对象? [英] How best to convert CakePHP date picker form data to a PHP DateTime object?

查看:105
本文介绍了如何最好将CakePHP日期选择器表单数据转换为PHP DateTime对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 app / views / mymodel / add.ctp 中执行此操作:

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

然后,在 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);
}

有绝对的做这个 - 我只是还没有找到CakePHP的方式...

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

我想做的是:

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


推荐答案

我知道这是一个老问题,但是如果有人来寻找答案:CakePHP的通用模型类有一个方法 :: deconstruct ),用于在内部处理这个必要的逻辑。您可以这样使用:

I know this is an old question, but in case anyone else comes looking for an answer: CakePHP's generic Model class has a method, ::deconstruct(), that is used to handle this necessary logic internally. You can use it like this:

$stringDate = $this->MyModel->deconstruct('fieldname', $arrayDate)

这篇关于如何最好将CakePHP日期选择器表单数据转换为PHP DateTime对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆