1

I am a beginner at CakePHP and I have o problem. I have to validate 2 dates(date_start & date_end). Date_end must be at least 30 days later than date_start. I am little confused of how to write the function for date difference. I don't know what parameteres I have to add and how to find them from the variable $validate. I also added another validation for the dates which tells date_end must be later than date_start and it works. Here's is the code of validations:

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){



}

Do you do you know the code I should write in function date_diff?

Thank you!

  • possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – ndm Mar 05 '15 at 15:35
  • There isn't any built in function for comparing dates, only number equalTo, greater than or less than. You'll need to create a custom validation rule which can either compare an epoch time or a DateTime object. – thepratt Mar 05 '15 at 15:36
  • @ndm This isn't a duplicate as he's looking to do this with CakePHP validation, not just compare dates. – thepratt Mar 05 '15 at 15:36
  • @nuc He already has a custom validation rule and is now asking "_Do you do you know the code I should write in function date_diff?_", and the linked possible duplicate explains exactly that. – ndm Mar 05 '15 at 15:44
  • @ndm You're right. I missed the sentence after the code quote. I need to sleep. – thepratt Mar 05 '15 at 18:16

1 Answers1

1

Thank you very much @ndm and the other guys of course for you help. I wrote this code in function diff and it worked:

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;



}