Welcome Guest, Not a member yet? Register   Sign In
How to work with timestampdiff?

Hi Friends,

Good Day!

I would like to ask some help in working with timestampdiff.

I wanted to get the time duration between the datastarted and dateresolved.
and the result will be automatically inserted into the database.

Below is my Report_Model Codes.
   function get_TimeDifference($id)
     $this->db->select('TIMESTAMPDIFF(HOUR,DateStarted,DateRestored) as OutageDuration',FALSE);
     $query = $this->db->get('network_outage');
     return $query;
Below also is my code in Controller

       function update_downtime($id)
           $data['title'] = 'Update Network Downtime';
           $user_id = $this->uri->segment(3);

           $data['fetch_single_outage'] = $this->network_model->fetch_single_outage($user_id);
           $data['fecth_downtime'] = $this->network_model->get_outagelist();
           $data['get_ISPStatus'] = $this->network_model->get_ISPStatus();

         function network_update_form_validation()
           $Outage = $this->report_model->get_TimeDifference($OutageDuration);
             $data  = array(
               'DateRestored' => $this->input->post('date_restored'),
               'Status' => $this->input->post('select_ticket_status'),
               'OutageDuration' => $Outage,
               'PerformedBy' => $this->session->userdata('user_name')


                 $this->session->set_flashdata('downtime_updated','Outage has been updated successfully');

Please see highlighted codes

Thank you and best regards,


...or as a suggestion. You could obtain the start and solution timestamps. Store them in the database and then use a database view to give you the difference. Basically, using the database to do the heavy lifting of figuring out the time difference.

Hi @php_rocs,
I don't get the logic what I wanted is.

I have the value of the DateStarted once I have inputted the value of DateResolved it will automatically get the timestampdiff or the outage duration and will insert the Outage duration into the database.


I'm saying first get both timestamp values into the database. Then all you have to do is create a database view that will take those two values and give you the difference (in a timestamp format). Pass that view to your controller which will pass it to your view. Then there will be no need to save the difference value in the database.

MySQL Tutorial Views

MySQL Views
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )

Hi Everyone,

Thanks for helping me out ,

Create a view on mysql did solve my problem

Theme © iAndrew 2016 - Forum software by © MyBB