'Validate that start time is greater than end time

I'm using Codeigniter and have two variables called event_start_time and event_end_time. I need to check if start time is greater than end time.

How could I validate this using the form validation library in Codeigniter?

$this->form_validation->set_rules('event_start_time', 'Starttid', 'required|strip_tags|trim');
$this->form_validation->set_rules('event_end_time', 'Sluttid', 'required|strip_tags|trim');


Solution 1:[1]

Hi There is no such option in CI.

You have to simple use Comparison operator like below:

if($event_start_time > $event_end_time){ /.../ }

Solution 2:[2]

There are a few ways you could approach this, but this was the first thing I would try (code untested).

Assuming this is CodeIgniter 3

1) Create the following config file at /application/config/validation/validate.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

// CI not normally available in config files,
// but we need it to load and use the model
$CI =& get_instance();

// Load the external model for validation of dates.
// You will create a model at /application/models/validation/time_logic.php
$CI->load->model('validation/time_logic');

$config['validation_rules'] = [
    [
        'field' => 'event_start_time',
        'label' => 'Starttid',
        'rules' => 'trim|required|strip_tags'
    ],
    [
        'field' => 'event_end_time',
        'label' => 'Sluttid',
        'rules' => [
            'trim',
            'required',
            'strip_tags'
            [ 
                '_ensure_start_time_end_time_logic', 
                function( $str ) use ( $CI ) {
                    return $CI->time_logic->_ensure_start_time_end_time_logic( $str ); 
                }
            ]
        ]
    ]
];

2) Create the validation model at /application/models/validation/time_logic.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Time_logic extends CI_Model {

    public function __construct()
    {
        parent::__construct();
    }

    public function _ensure_start_time_end_time_logic( $str )
    {
        // Making an assumption that your posted dates are a format that can be read by DateTime
        $startTime = new DateTime( $this->input->post('event_start_time') );
        $endTime = new DateTime( $str );

        // Start time must be before end time
        if( $startTime >= $endTime )
        {
            $this->form_validation->set_message(
                '_ensure_start_time_end_time_logic', 
                'Start time must occur before end time'
            );
            return FALSE;
        }

        return $str;
    }

}

3) In your controller, model, or wherever it is that you are validating the post, load and apply the validation rules, instead of specifying them how you were doing it.

$this->config->load('validation/validate');
$this->form_validation->set_rules( config_item('validation_rules') );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ashish Tiwari
Solution 2 Brian Gottier