cakephp中两个日期验证之间的差异

时间:2015-03-05 15:27:32

标签: cakephp cakephp-1.3 cakephp-2.1 cakephp-2.3 cakephp-appmodel

我是CakePHP的初学者,我有问题。我必须验证2个日期(date_start& date_end)。 Date_end必须至少比date_start晚30天。我对如何编写日期差异函数感到困惑。我不知道我必须添加什么参数以及如何从变量$ validate中找到它们。我还为日期添加了另一个验证,告诉date_end必须晚于date_start并且它有效。以下是验证代码:

public $validate = array(
    'user_id' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
        ),
    ),
    'name' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
        ),
    ),

    'date_start' => array(
        'rule' => 'date',
        'message' => 'Enter a valid date'
    ),

    'date_end' => array(
    'date' => array(
      'rule' => array('date'), 
      'message' => 'The date must be valid'),
    'dates' => array(
        'rule' => 'dates', 
        'message' => 'The end date must be later tha start date'),
  ),

);

public function dates(){ 

    if($this->data['Lesson']['date_end'] > $this->data['Lesson']['date_start']){
        return true;
    }
    else{
        return false;
    }

}

public function date_diff($date_start, $date_end){



}

你知道我应该在函数date_diff写的代码吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

非常感谢@ndm和其他人当然可以帮到你。我在函数diff中编写了这段代码并且工作正常:

public function date_diff($check1,$ckeck2){
    $check1 = $this->data['Lesson']['date_start'];
    $check2 = $this->data['Lesson']['date_end'];
    $diff = abs(strtotime($check2) - strtotime($check1));
    if($diff >= 2592000){
        return true;
    }
    else
        return false;



}
相关问题