2

I am using codeigniter. In my controller file I need to get the date of hire and date of termination for an employee and check that date of hire is lesser than date of termination. My controller code is given below:

 public function add()
{
    //if save button was clicked, get the data sent via post
    if ($this->input->server('REQUEST_METHOD') === 'POST')
    {

        //$this->load->library('session');
        //$this->session->unset_userdata('tax_details');
        //form validation
                    $this->form_validation->set_rules('id', 'id');

        $this->form_validation->set_rules('emp_first_name', 'emp_first_name','alpha');
        $this->form_validation->set_rules('emp_last_name', 'emp_last_name','alpha');
        $this->form_validation->set_rules('emp_email_id', 'emp_email_id');
        $this->form_validation->set_rules('emp_emergency_contact', 'emp_emergency_contact');
        $this->form_validation->set_rules('category', 'category');
        $this->form_validation->set_rules('emp_id_card', 'emp_id_card');
        $this->form_validation->set_rules('emp_time_in', 'emp_time_out');
        $this->form_validation->set_rules('emp_date_of_hire', 'emp_date_of_hire');
        $this->form_validation->set_rules('emp_date_of_termination', 'emp_date_of_termination');
        $this->form_validation->set_rules('emp_date_of_rehire', 'emp_date_of_rehire');
        $this->form_validation->set_rules('emp_reference_num', 'emp_reference_num');
        $this->form_validation->set_rules('emp_service_limitation', 'emp_service_limitation');
        $this->form_validation->set_rules('chair_renter', 'chair_renter');
        $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');

        //if the form has passed through the validation
        if ($this->form_validation->run())
        {
            $data_to_store = array(
                 'id' => $this->input->post('id'),
                'emp_first_name' => $this->input->post('emp_first_name'),
                'emp_last_name' => $this->input->post('emp_last_name'),
                'emp_email_id' => $this->input->post('emp_email_id'),
                'emp_emergency_contact' => $this->input->post('emp_emergency_contact'),          
                'category' => $this->input->post('category'),
                'emp_id_card' => $this->input->post('emp_id_card'),
                'emp_time_in' => $this->input->post('emp_time_in'),
                'emp_time_out' => $this->input->post('emp_time_out'),
                'emp_date_of_hire' => $this->input->post('emp_date_of_hire'),
                'emp_date_of_termination' => $this->input->post('emp_date_of_termination'),
                'emp_date_of_rehire' => $this->input->post('emp_date_of_rehire'),
                'emp_reference_num' => $this->input->post('emp_reference_num'),
                'emp_service_limitation' => $this->input->post('emp_service_limitation'),
                'chair_renter' => $this->input->post('chair_renter'),

            );
            if($data_to_store['emp_date_of_hire'] != NULL || $data_to_store['emp_date_of_termination'] != NULL || $data_to_store['emp_date_of_rehire'] != NULL)
            {
                $data_to_store['emp_date_of_hire'] = str_replace('/', '-', $data_to_store['emp_date_of_hire'] );
                $date1 = date("Y-m-d", strtotime($data_to_store['emp_date_of_hire']));
                $data_to_store['emp_date_of_termination'] = str_replace('/', '-', $data_to_store['emp_date_of_termination'] );
                $date2 = date("Y-m-d", strtotime($data_to_store['emp_date_of_termination']));  
                $data_to_store['emp_date_of_rehire'] = str_replace('/', '-', $data_to_store['emp_date_of_rehire'] );
                $date3 = date("Y-m-d", strtotime($data_to_store['emp_date_of_rehire']));  
                if($date1 < $date2 && $date1 < $date3)
                {
                 if($this->employee_model->store_employee($data_to_store)){
                $data['flash_message'] = TRUE; 
                $this->session->set_flashdata('flash_message', 'updated');
                //$this->session->set_userdata('tax_details', ['total'=>$this->input->post('id'),"tax"=>$this->input->post('emp_service_limitation'),"renter"=>$this->input->post('chair_renter')]);
               //redirect(base_url().'admin/employee/view');



               if($data_to_store['chair_renter'] == "yes")
            {
                $datas = array(
                'emp_id'=>$data_to_store['id'],
                'emp_first_name'=>$data_to_store['emp_first_name'],
                'emp_last_name'=>$data_to_store['emp_last_name'],
                'category'=>$data_to_store['category'],
                );
                $this->employee_model->save_chair_renter($datas);
            }

            }else{
                $data['flash_message'] = FALSE; 
            }   
                }
                else
                {
                 $data['flash_message'] = FALSE; 
                }

            //if the insert has returned true then we show the flash message
    }
    else
    {

     if($this->employee_model->store_employee($data_to_store)){
                $data['flash_message'] = TRUE; 
                $this->session->set_flashdata('flash_message', 'updated');
                //$this->session->set_userdata('tax_details', ['total'=>$this->input->post('id'),"tax"=>$this->input->post('emp_service_limitation'),"renter"=>$this->input->post('chair_renter')]);
               //redirect(base_url().'admin/employee/view');



               if($data_to_store['chair_renter'] == "yes")
            {
                $datas = array(
                'emp_id'=>$data_to_store['id'],
                'emp_first_name'=>$data_to_store['emp_first_name'],
                'emp_last_name'=>$data_to_store['emp_last_name'],
                'category'=>$data_to_store['category'],
                );
                $this->employee_model->save_chair_renter($datas);
            }

            }else{
                $data['flash_message'] = FALSE; 
            }   
    }


        }

    }
    //fetch manufactures data to populate the select field
    $data['designation'] = $this->designation_model->get_designation();
    //load the view
    $data['main_content'] = 'admin/employee/add';
    $this->load->view('includes/template', $data);  
}  

I need to check whether date field is empty. In my view file I get the input as given below.

      <input type="date" name="emp_date_of_hire" value="<?php echo set_value('emp_date_of_hire'); ?>">
  <input type="date" name="emp_date_of_termination" value="<?php echo set_value('emp_date_of_termination'); ?>">

How to code? Can someone help me?

Anu
  • 630
  • 1
  • 21
  • 45
  • You compare 2 equal date's, is that intended? – jmattheis Jul 07 '15 at 08:48
  • it does not work for me$data_to_store['emp_date_of_hire'] = str_replace('/', '-', $data_to_store['emp_date_of_hire'] ); $date1 = date("Y-m-d", strtotime($data_to_store['emp_date_of_hire'])); $data_to_store['emp_date_of_termination'] = str_replace('/', '-', $data_to_store['emp_date_of_termination'] ); $date2 = date("Y-m-d", strtotime($data_to_store['emp_date_of_hire'])); if($date1 < $date2) {//some code} – Anu Jul 07 '15 at 08:57
  • can someone help me check whether entered date is not null – Anu Jul 07 '15 at 09:12

0 Answers0