title: find production time based on start time and end time php
body: <p>I have to find the production time of an employee, how many hours and minutes he takes to complete the project.</p>
  
  <p>My condition is,</p>
  
  <p><code>1. I have start and end datetime of an employee</code></p>
  
  <p><code>2. I have 3 break time.</code></p>
  
  <p><code>3. The break times should be subtracted if break times is in between start and end time.</code></p>
  
  <p><code>4. The working time is between 08:30 to 18:30, the time beyond this working time should be subtracted.</code></p>
  
  <p>All the above conditions should be achieved. I have tried to achieve this but for one day work the code is working fine. But for more than one day the break this is subtracted only once and working time not subtracted.</p>
  
  <pre><code>$start = "01-01-2018 09:00";
  $end = "02-01-2018 18:30";
  
  $break1_start = "11:00";
  $break1_end = "11:15";
  
  $break2_start = "13:00";
  $break2_end = "13:30";
  
  $break3_start = "16:00";
  $break3_end = "16:15";
  
  //work time
  $break4_start = "18:30";
  $break4_end = "08:30";
  
  $break1 = 0;
  $break2 = 0;
  $break3 = 0;
  $break4 = 0;
  
  //Break 1
  if($break1_start &gt;= date("H:i", strtotime($start)) and $break1_start &lt;= date("H:i", strtotime($end))) {
      if($break1_end &gt;= date("H:i", strtotime($start)) and $break1_end &lt;= date("H:i", strtotime($end))) {
          $break1 = strtotime($break1_end) - strtotime($break1_start);
      }
      else {
          $end = date("H:i", strtotime($end));
          $break1 = strtotime($break1_end) - strtotime($end);
      }
  }
  else if($break1_end &gt;= date("H:i", strtotime($start)) and $break1_end &lt;= date("H:i", strtotime($end))) {
      $start = date("H:i", strtotime($start));
      $break1 = strtotime($break1_end) - strtotime($start);
  }
  
  //Break 2
  if($break2_start &gt;= date("H:i", strtotime($start)) and $break2_start &lt;= date("H:i", strtotime($end))) {
      if($break2_end &gt;= date("H:i", strtotime($start)) and $break2_end &lt;= date("H:i", strtotime($end))) {
          $break2 = strtotime($break2_end) - strtotime($break2_start);
      }
      else {
          $end = date("H:i", strtotime($end));
          $break2 = strtotime($break2_end) - strtotime($end);
      }
  }
  else if($break2_end &gt;= date("H:i", strtotime($start)) and $break2_end &lt;= date("H:i", strtotime($end))) {
      $start = date("H:i", strtotime($start));
      $break2 = strtotime($break2_end) - strtotime($start);
  }
  
  //Break 3
  if($break3_start &gt;= date("H:i", strtotime($start)) and $break3_start &lt;= date("H:i", strtotime($end))) {
      if($break3_end &gt;= date("H:i", strtotime($start)) and $break3_end &lt;= date("H:i", strtotime($end))) {
          $break3 = strtotime($break3_end) - strtotime($break3_start);
      }
      else {
          $end = date("H:i", strtotime($end));
          $break3 = strtotime($break3_end) - strtotime($end);
      }
  }
  else if($break3_end &gt;= date("H:i", strtotime($start)) and $break3_end &lt;= date("H:i", strtotime($end))) {
      $start = date("H:i", strtotime($start));
      $break3 = strtotime($break3_end) - strtotime($start);
  }
  
  //Work time
  if($break4_start &gt;= date("H:i", strtotime($start)) and $break4_start &lt;= date("H:i", strtotime($end))) {
      if($break4_end &gt;= date("H:i", strtotime($start)) and $break4_end &lt;= date("H:i", strtotime($end))) {
          $break4 = strtotime($break4_end) - strtotime($break4_start);
      }
      else {
          $end = date("H:i", strtotime($end));
          $break4 = strtotime($break4_end) - strtotime($end);
      }
  }
  else if($break4_end &gt;= date("H:i", strtotime($start)) and $break4_end &lt;= date("H:i", strtotime($end))) {
      $start = date("H:i", strtotime($start));
      $break4 = strtotime($break4_end) - strtotime($start);
  }
  
  $diff = strtotime($end) - strtotime($start);
  $diff = $diff - $break1 - $break2 - $break3 - $break4;
  
  $hours = floor($diff / 3600);
  $minutes = floor(($diff / 60) % 60);
  $seconds = $diff % 60;
  
  echo "$hours:$minutes:$seconds";
  </code></pre>
  
  <p>From the above code,</p>
  
  <p><code>1. All the break time should be subtracted twice because break time comes twice in the given start and end time.</code></p>
  
  <p>The <code>output</code> for the above given start and end time and all break time should be <code>17:30:00</code></p>
  
  <p>But now the output for the above code is <code>1458:30:0</code>.</p>
  
  <p>How to achieve my conditions and output? I am really stuck here. Could any one help me out. Thank you.</p>