2015-09-26 45 views
-2

我有一個數據庫表,其中離開將被更新。計算從表中留下了超過1個休假進入

它有以下欄目:

id | empno | startdate | enddate | status | duration 

現在我要計算在給定的月和年休假,其中年份和月份將來自用戶的輸入。我有一個代碼將檢索不。葉在給定的月份,如果休假項是1

的問題是,離開1倍以上,如果用戶已經採取,該功能只計算從數據庫中的最後一排。任何幫助,將不勝感激。

<?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` FROM `leaves` WHERE `employee` = '2' AND `status` = '3' 
AND `startdate` > '01-09-2015' AND `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(); 
} 
?> 
+2

如果可能的話,您可以提供更多的代碼嗎?我知道你已經展示了很多,但只是多一點 – Drew

+0

@Drew這是代碼..... –

回答

1

只是改變了SQL查詢到

$ SQL =「SELECT SUM(DATEDIFF(最低(結束日期, '二○一五年九月三十〇日'), GREATEST(開始日期,「2015-09 -01'))+ 1)days FROM leaves WHERE startdate < ='2015-09-30'AND enddate> ='2015-09-01'AND employee ='2' AND status ='1'「;

UFFF!得到了結果。