本文介紹了如何最好地將 CakePHP 日期選擇器表單數(shù)據(jù)轉(zhuǎn)換為 PHP DateTime 對(duì)象?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我在 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);
}
絕對(duì)必須有更好的方法來(lái)做到這一點(diǎn) - 我只是還沒(méi)有找到 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
}
推薦答案
我知道這是一個(gè)老問(wèn)題,但萬(wàn)一其他人來(lái)尋找答案:CakePHP 的通用 Model
類有一個(gè)方法, ::deconstruct()
,用于在內(nèi)部處理這個(gè)必要的邏輯.你可以這樣使用它:
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)
這篇關(guān)于如何最好地將 CakePHP 日期選擇器表單數(shù)據(jù)轉(zhuǎn)換為 PHP DateTime 對(duì)象?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!