在我的应用程序中,我有一个jQuery日期选择器,用于在两个单独的字段中插入2个日期(开始和结束日期),用作日期范围。我想将此2个日期存储在mysql数据库中,以便我可以检索和使用它们在fullCalendar jQuery插件中。 我的问题是,当我插入日期并在编辑页面中按“提交”时,我收到一条消息,提示我建议的值无效。
我如何以d-m-y格式插入日期并将其以y-m-d格式存储在数据库中?
我的观点
echo $this->Form->control('starting_date', ['type' => 'text', 'class' => 'datepicker']);
echo $this->Form->control('ending_date', ['type' => 'text', 'class' => 'datepicker']);
模型
$validator
->date('starting_date')
->requirePresence('starting_date', 'create')
->notEmpty('starting_date');
$validator
->date('ending_date')
->requirePresence('ending_date', 'create')
->notEmpty('ending_date');
控制器
public function edit($id = null)
{
$setting = $this->Settings->get($id);
if ($this->request->is(['patch', 'post', 'put'])) {
$setting = $this->Settings->patchEntity($setting, $this->request->getData());
if ($this->Settings->save($setting)) {
$this->Flash->success(__('The date range has been updated.'));
return $this->redirect(['action' => 'view/1']);
}
$this->Flash->error(__('The date range could not be saved. Please, try again.'));
}
$this->set(compact('setting'));
}
脚本
<script>
$( function() {
$( ".datepicker" ).datepicker({
dateFormat: 'dd/mm/yy'});
} );
</script>