how to get DAYS absent from working days from given date range?
<p>This was the function I made for getting list of leavedays which an employee have taken in a given date range. It's fine if leaves taken are one or two, but its too complicated so that, it takes much time to retrieve results hence causes time out error! Any help?   </p>

<p>This is the function:</p>

<pre><code>function dates_between($emp_id, $start_date, $end_date) 
 {

    $day_incrementer = 1;
    $count_leaves = 0;
    $flag = 0;

    // Getting the days from DB where the employee '28' had worked in given date range

    $work_res = mysql_query("SELECT DISTINCT date FROM `work_details` WHERE  employee_id='28' and date between '2012-02-01' and '2012-02-29'");

    do {
        while($row = mysql_fetch_array($work_res)) 
             {
           while((date("Y-m-d",$start_date) &lt; $row['date']) &amp;&amp; ($flag = 0)) 
                       // loop to find  startdate  less than table date! if table date(attendance) is starting from 3, we need to print leaves 1,2  if they are not  weekends
                   {
                 if(!(date('N', strtotime(date("Y-m-d", $start_date))) &gt;=6)) 
                       {    
                               //checking for weekends, prints only weekdays
                    echo date("Y-m-d", $start_date) . " \n ";
                    $count_leaves++;
               }

           $start_date = $start_date + ($day_incrementer * 60 * 60 *24);              
            }

            $flag=1;


    while((date("Y-m-d",$start_date) != $row['date']))
     // loop to print $start_date,which is not equal to table date
    {
    if(!(date('N', strtotime(date("Y-m-d", $start_date))) &gt;= 6)) 
      {
        echo  date("Y-m-d", $start_date) . "\n";
        $count_leaves++;
      }
     $$start_date = $start_date + ($day_incrementer * 60 * 60 * 24);
     }

        $start_date = $start_date + ($day_incrementer * 60 * 60 * 24);
    }

 // loop to print $start_date,comes rest after tabledate if tabledate finishes with 28, prints rest of dates 29,30
  if(!(date('N', strtotime(date("Y-m-d", $start_date))) &gt;= 6) &amp;&amp; ($start_date &lt;= $end_date))
  {
            echo  date("Y-m-d", $start_date) . "\n";
            $count_leaves++;
            $start_date = $start_date + ($day_incrementer * 60 * 60 * 24);
  }


  } while($start_date &lt;= $end_date);

    return($count_leaves);
 }
</code></pre>
