Welcome Guest, Not a member yet? Register   Sign In
Validate one date against another
#1

[eluser]jalalski[/eluser]
I have a form with two date fields, I want to validate that the end date is later than the start date. What's the best way to do that? And is it even possible with form_validater?

TIA.
#2

[eluser]Xeoncross[/eluser]
convert them to timestamps (if they are within 1970-2038) and then make sure the later is "bigger" than the former.

strtotime();
#3

[eluser]fesweb[/eluser]
Use a callback function in the validation.

Code:
// OLD validation class version
$this->rules[0]['start_date']     = "trim|required";
$this->rules[0]['end_date']     = "trim|required|callback_end_date_check";

// NOT TESTED new validation class version v 1.7
$this->form_validation->set_rules('start_date', 'Start Date', 'trim|required|callback_start_date_check');
$this->form_validation->set_rules('end_date', 'End Date', 'trim|required|callback_start_date_check');

The callback function lives in the same controller, and goes something like this (cobbled together example, not tested).
Code:
function end_date_check()
{
    if($this->input->post('start_date') >= $this->input->post('end_date'))
    {
        $this->validation->set_message('end_date', 'End date must occur AFTER the start date.');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}
#4

[eluser]dmorin[/eluser]
Of course
Code:
if($this->input->post('start_date') >= $this->input->post('end_date'))
will only work if they are in unix timestamps. So depending on the date format, you may need to do what Xeoncross suggested:
Code:
if(strtotime($this->input->post('start_date')) < strtotime($this->input->post('end_date')))

Edit: oops, wrong sign for end date being later than the start date. End date would have to be Greater than start date in that case...
#5

[eluser]xwero[/eluser]
The best way i can think of is to validate if the two dates have the same format and then validate the end date to see it has a value in the future
Code:
$this->form_validation->set_rules('start_date','From','valid_date');
$this->form_validation->set_rules('end_date','To','valid_date|greater_date[start_date]');
the valid_date and greater_date methods can be added to extended form_validation class, i think those functions are common enough.
#6

[eluser]fesweb[/eluser]
Thanks for reiterating the date formatting issue.

I meant to mention that part and then forgot too..., but my main point was to show the callback function and how it's called in the validation process.

Also, before you compare the dates, you should check the validity of the date itself (does that day actually exist as entered) using php's checkdate ( int $month , int $day , int $year ). It's good for sniffing out leap year issues, etc.
#7

[eluser]jalalski[/eluser]
Thanks for the replies. My question concerned mainly using the callback_ functionality. That allows only to post one fields data, but I ended up using the POST values. Didn't realise that they were available to me at that point, but it seems that they are.

Thanks again.




Theme © iAndrew 2016 - Forum software by © MyBB