⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

How to Validate two dates



Lucas
  • profile picture
  • Member

Posted 01 May 2012 - 02:33 AM

how [color=#333333][font=arial, sans-serif][size=4]to validate[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]that the second[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]date[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]is not less[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]or[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]equal to the first[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]?[/size][/font][/color]

[img]http://content.screencast.com/users/Lucasturilli/folders/Jing/media/63da5893-1e55-48d1-9194-10dc9055c05f/2012-04-30_2130.png[/img]

Lucas
  • profile picture
  • Member

Posted 17 May 2012 - 15:32 PM

Somebody HELP !!!!!!!!!!!!!!!!!!!!!!11

Lucas
  • profile picture
  • Member

Posted 23 May 2012 - 22:04 PM

Somebody

kenvogt
  • profile picture
  • Member

Posted 24 May 2012 - 18:07 PM

Assuming that you are using the field names 'fecha1' and 'fecha2', add the following to your controller. Prior to:

$crud->render();

enter:


$crud->set_rules('fecha2','Fecha de Finalización','callback_check_dates[fecha1]');

Then add this function to your controller:

public function check_dates($fecha2, $fecha1)
{
if ($fecha2 >= $fecha1)
{
return TRUE;
}
else
{
$this->form_validation->set_message('check_dates', "La fecha de inicio no puede ser posterior a la fecha de finalización.");
return FALSE;
}
}


(Sorry about all the edits, the interface hates me.)

eritovs
  • profile picture
  • Member

Posted 10 June 2012 - 06:52 AM

I tested several times but it does not work ...
What I'm doing wrong?


...
$this->grocery_crud->set_rules('periodto','Period to','callback_check_dates["periodfrom"]');
$output = $this->grocery_crud->render();
...
public function check_dates($datet2,$date1)
{
if ($datet2 >= $date1)
{
return TRUE;
}
else
{
$this->form_validation->set_message('check_dates', $datet2.' date 2 is smaller then '.$date1.' date 1.');
return FALSE;
}
}


Date field $date1 appiers as word "periodfrom" not as value.
How to feed second value correctly to the callback function?

Lucas
  • profile picture
  • Member

Posted 21 June 2012 - 22:02 PM

No Wokr the second value

La fecha de inicio no puede ser posterior a la fecha de finalización.etapa_fecha_inicio07/06/2012

Luan
  • profile picture
  • Member

Posted 23 June 2012 - 02:04 AM

Since its a callback before upload, the GC_library didnt organized the format of the date yet.

You gotta make sure the data is in this format: YYYY-mm-dd , like 2012-06-22.

Try read more about explode and join on the internet.

Try this:

public function check_dates($fecha2, $fecha1)
{
$partes = explode('/', $this->input->post('fecha1'));
$fecha1 = join('-', $partes);

$partes2 = explode('/', $this->input->post('fecha2'));
$fecha2 = join('-', $partes2);

if ($fecha2 >= $fecha1)
{
return TRUE;
}
else
{
$this->form_validation->set_message('check_dates', "La fecha de inicio no puede ser posterior a la fecha de finalización.");
return FALSE;
}
}

manushimn
  • profile picture
  • Member

Posted 15 August 2014 - 21:42 PM

Thanks alot. This worked for me :)


Arjunraj
  • profile picture
  • Member

Posted 01 September 2017 - 05:17 AM

This works only when we enter the dates of the same month, If I enter 1/08/2017 and 28/07/2017 , It should show an error message(end date should be greater than start date) but it is actually inserting the dates in to the database , Can anyone help me with this..?


Arjunraj
  • profile picture
  • Member

Posted 01 September 2017 - 06:08 AM

I found it , This will work for all cases,

     $crud->set_rules('end_date', 'end_date', 'trim|callback_check_dates['.$this->input->post('start_date') .']');

 

 

  // Callback function

public function check_dates($end_date, $start_date) { 
        $parts = explode('/', $this->input->post('start_date'));
        $start_date = join('-', $parts);
        $start_date=  strtotime($start_date);
 
        $parts2 = explode('/', $this->input->post('end_date'));
        $end_date = join('-', $parts2);
        $end_date= strtotime($end_date);
        
        if ($end_date >= $start_date) {
            
            return true;
        } else {
            $this->form_validation->set_message('check_dates', "end date should be greater than start date");
            return false;
        }
    }