php - Calculate leaves from table for more than 1 leave entry -


i have database table in leave updated.

it has following columns:

id | empno | startdate | enddate | status | duration 

now have calculate leave in given month , year month , year come user input. have code retrieve no. of leaves in given month if leave entry 1.

the problem if user has taken leave more 1 time, function calculating last row database. appreciated.

<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "lms"; $conn = mysqli_connect($servername, $username, $password, $dbname); $empid    = (isset($_post['empid'])    ? $_post['empid']    : ''); $month = (isset($_post['month'])    ? $_post['month']    : '');  $month = "2015-sep";   $month1 = date('f', strtotime('$month')); //echo $month1;  $year = date('y',strtotime('$month'));  $monthstart = date("y-m-1") . "<br/>"; $num = cal_days_in_month(cal_gregorian, date("m"), date("y")); $monthend = date("y-m-".$num)."<br/>"; //echo "$num"; $months = date('m'); $years = date ('y'); $leave = 0;  if ($conn->connect_error)  {     die("connection failed: " . $conn->connect_error); }  $sql = "select  `startdate`, `enddate`,`duration` `leaves` `employee` = '2' , `status` = '3'  , `startdate` > '01-09-2015' , `enddate` < '30-09-2015' ";  $result = $conn->query($sql);  if ($result->num_rows > 0) {     // output data of each row     while($row = $result->fetch_assoc())  {         $start = ($row['startdate']);         $end =  ($row['enddate']);         $startcount = ['count(`employee`)'];         //$durationlvs =  ($row['duration']);            echo "sdate:$start,edate:$end</br>";             //echo "lvsdur:$durationlvs";     }      function getleavesinperiod($start, $end) {         $date = new datetime($start);         $enddate = new datetime($end);         $leaves = array();          while( $date <= $enddate ) {             $year = $date->format('y');             $month = $date->format('m');                      if(!array_key_exists($year, $leaves))                 $leaves[$year] = array();             if(!array_key_exists($month, $leaves[$year]))                 $leaves[$year][$month] = 0;              $leaves[$year][$month]++;                        $date->modify("+1 day");         }     return $leaves; } $leaves = getleavesinperiod($start,$end); $noofleaves=$leaves[$years][$months]; echo $noofleaves; $conn->close(); }    ?> 

just changed sql query

$sql = "select sum(datediff(least(enddate, '2015-09-30'), greatest(startdate, '2015-09-01'))+1) days leaves startdate<='2015-09-30' , enddate>='2015-09-01' , employee='2' , status='1'";

ufff!!! got result.


Comments