0

我有这个问题,我使用的是 cake 2.8.x,我的 cakephp 应用程序没有保存日期字段的选定值。即使您选择了 "2016-02-02" ,它也会一直保存 "1970-01-01" 。有什么问题?

这是我在控制器中的代码

$this->request->data['Jobapp']['dayofbirth']= date('Y-m-d H:i:s', strtotime($this->request->data['Jobapp']['dayofbirth']));
$this->request->data['Jobapp']['dateofissue']= date('Y-m-d H:i:s', strtotime($this->request->data['Jobapp']['dateofissue']));
$this->request->data['Jobapp']['expirydate']= date('Y-m-d H:i:s', strtotime($this->request->data['Jobapp']['expirydate']));

这是视图文件中的字段之一

<td><?php echo $this->Form->input('dayofbirth',array('label'=>'Date of Birth','type'=>'date','minYear'=>date('Y') - 90))?>

可能是什么问题?

    'dayofbirth' => array(
        'day' => '12',
        'month' => '08',
        'year' => '1993'
    ),
    'maritalstatus' => 'emtpy',
    'nationality' => 'empty',
    'complexion' => '',
    'passportnumber' => '',
    'dateofissue' => array(
        'month' => '05',
        'day' => '25',
        'year' => '2016'
    ),
    'placeofissue' => '',
    'expirydate' => array(
        'month' => '05',
        'day' => '25',
        'year' => '2016'
    ),
4

3 回答 3

0

您的日期字段是数组。尝试:

 $this->request->data['Jobapp']['dayofbirth']= date('Y-m-d H:i:s', strtotime(implode('-',$this->request->data['Jobapp']['dayofbirth'])));

与其他领域相同

于 2016-05-25T14:14:44.150 回答
0

您不需要修改日期字段,CakePHP 会处理它。所以你可以摆脱这些行:

 $this->request->data['Jobapp']['dayofbirth']= date('Y-m-d H:i:s', strtotime($this->request->data['Jobapp']['dayofbirth']));
 $this->request->data['Jobapp']['dateofissue']= date('Y-m-d H:i:s', strtotime($this->request->data['Jobapp']['dateofissue']));
 $this->request->data['Jobapp']['expirydate']= date('Y-m-d H:i:s', strtotime($this->request->data['Jobapp']['expirydate']));
于 2016-05-25T19:27:13.097 回答
0

您可以借助 jquery ui 来避免使用控制器修改日期字段。

在您看来,请进行以下更改,

<!-- Load the jquery & jquery ui library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script>
    $(function(){
        $(".datepicker").datepicker({dateFormat:"yyyy-mm-dd"});
    });
</script>
<?php echo $this->Form->input('dayofbirth',['class'=>'datepicker']); ?>
<?php echo $this->Form->input('dateofissue',['class'=>'datepicker']); ?>
<?php echo $this->Form->input('expirydate',['class'=>'datepicker']); ?>

请删除处理输入数据的控制器代码。然后,您应该能够以正确的格式插入日期。

于 2016-05-26T16:16:05.043 回答