2012-06-08 16 views
3

這是我爲獲取僱員在給定日期範圍內參加的工作日的列表所做的功能。如果葉子是一個或兩個,這很好,但它太複雜了,因此需要很長時間來檢索結果,從而導致超時錯誤!任何幫助?如何在特定日期範圍內從工作日中缺席DAYS?

這是函數:

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) < $row['date']) && ($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))) >=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))) >= 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))) >= 6) && ($start_date <= $end_date)) 
    { 
      echo date("Y-m-d", $start_date) . "\n"; 
      $count_leaves++; 
      $start_date = $start_date + ($day_incrementer * 60 * 60 * 24); 
    } 


    } while($start_date <= $end_date); 

    return($count_leaves); 
} 
+0

你如何儲存假期? – slash197

+2

您的代碼被格式化爲_horribly_。請格式化它,使其實際上處於可讀狀態。 – Bojangles

+0

您是否在考勤表中存儲?你可以在那裏檢查缺席。 – Saqueib

回答

0

我注意到,你還問類似的問題在其他地方(http://stackoverflow.com/questions/10898293/how-to-get-days-of-leave-採取-IN-A-給出個月)。現在我試着深入你的代碼,以便對你正在嘗試的內容有一個基本的瞭解。請原諒我,如果我的回答不完全符合你的願望,因爲閱讀別人的想法並不容易。基本上,我所做的就是準備一個你想做的樣例代碼。此代碼需要特定工作人員在特定月份和年份工作的日期數組。然後繼續獲得當年那個月的所有工作日期。兩個數組的差異給出了工人缺席的日期(由於休假或AWOL)。公共假期沒有被考慮,但當然,您可以輕鬆修改代碼以添加該代碼。如果您在另一個數組中持有公共假日日期並將其與第一個結果進行區分,則最終數組將爲您提供您想要的結果。

現在,只是一個警告的提示,這段代碼是基本的,如果兩個數組並不完全相同的日期格式,數組差異將會失敗。就我個人而言,我會編寫自己的比較回調函數來比較各個日期並將其傳遞到array_udiff()以最大限度地確定。我很確定你可以處理。我只提供了基礎知識。自由使用並根據您的情況適當擴展。足夠的交談,請參閱下面的代碼示例。

<?php 
/*************************************************************************** 
* how to get DAYS absent from working days from given date range? 
* @Author Prof. No Time - 12th/June/2012 
****************************************************************************/ 

//Leave was 10th, 13th, 23rd, 24th 
//Note that 01-02-2012 is NOT exactly same as 1-2-2012; Important for the array_diff fxn used below. 
//Note Format is d-m-Y 
//Note I am assuming you have pulled this from a database of course 
$imaginaryWorkDatesOfWorker1 = array(
    '01-02-2012', '02-02-2012', '03-02-2012', '06-02-2012', '07-02-2012', '08-02-2012', 
    '09-02-2012', '14-02-2012', '15-02-2012', '16-02-2012', '17-02-2012', '20-02-2012', 
    '21-02-2012', '22-02-2012', '27-02-2012', '28-02-2012', '29-02-2012'  
); 

$leaveDays1 = getLeaveDays(2, 2012, $imaginaryWorkDatesOfWorker1); 
displayWorkersLeaveDays($leaveDays1); 

//Leave was 2nd, 16th, 19th, 23rd and 26th 
$imaginaryWorkDatesOfWorker2 = array(
    '01-03-2012', '05-03-2012', '06-03-2012', '07-03-2012', '08-03-2012', '09-03-2012', 
    '12-03-2012', '13-03-2012', '14-03-2012', '15-03-2012', '20-03-2012', '21-03-2012', 
    '22-03-2012', '27-03-2012', '28-03-2012', '29-03-2012', '30-03-2012' 
); 

$leaveDays2 = getLeaveDays(3, 2012, $imaginaryWorkDatesOfWorker2); 
displayWorkersLeaveDays($leaveDays2); 



///MAIN FUNCTION TO GET LEAVE DATES/// 
function getLeaveDays($month, $year, $arrDatesPresent=array()){ 
    $arrAllWorkDatesInMonth = getDatesInTheMonth($month, $year); 

    //var_dump($arrDatesPresent); var_dump($arrAllWorkDatesInMonth); 

    $leaveDays = array_diff($arrAllWorkDatesInMonth, $arrDatesPresent); 
    return $leaveDays; 
} 


///HELPER FUNCTIONS/// 
/** 
* <p>Gets all the dates in a given month in the specified year. default format d-m-Y<p> 
* @param int $month 
* @param int $year 
* @param boolean $includeWeekends 
* @param string $format2Use 
* @throws Exception if invalid parameters are given 
* @return array: dates in the given month, in the given year 
*/ 
function getDatesInTheMonth($month, $year, $includeWeekends=false, $format2Use='d-m-Y') { 
    $arrDatesInTheMonth = array(); 
    if (empty($format2Use)) $format2Use = 'm-d-Y'; 

    if (empty($month) || empty($year)){ 
    throw new Exception("Invalid parameters given."); 
    } 
    else{ 
    $fauxDate = mktime(0, 0, 0, $month, 1, $year); 
    $numOfDaysInMonth = date('t', $fauxDate); 

    if (!empty($numOfDaysInMonth)){ 
     for ($day = 1; $day <= $numOfDaysInMonth; $day++){ 

      $timeStamp = mktime(0, 0, 0, $month, $day, $year); 
      $cdate = date($format2Use, $timeStamp); 

      if ($includeWeekends){ 
       $arrDatesInTheMonth[] = $cdate; 
      } 
      else{ 
       if (!isWeekend($cdate)) { $arrDatesInTheMonth[] = $cdate; } 
      } 
     } 
    } 
    } 

    return $arrDatesInTheMonth; 
} 

/** 
* Checks if given date is a weekend use this if you have PHP greater than v5.1. 
* Credit: http://stackoverflow.com/users/298479/thiefmaster 
* @param date $date 
* @return boolean 
*/ 
function isWeekend($date) { 
    return (date('N', strtotime($date)) >= 6); 
} 


/** 
* Checks if given date is a weekend use this if you have PHP less than v5.1. 
* Credit: http://stackoverflow.com/users/298479/thiefmaster 
* @param date $date 
* @return boolean 
*/ 
function isWeekend2($date) { 
    $weekDay = date('w', strtotime($date)); 
    return ($weekDay == 0 || $weekDay == 6); 
} 

function printDates($arrDates){ 
    foreach ($arrDates as $key => $cdate) { 
     $display = sprintf('%s <br />', date('[l] - jS \of F Y', strtotime($cdate))); 
     echo $display; 
    } 
} 

function displayWorkersLeaveDays($leaveDays){ 
    echo '<div style="background-color:#CCC;margin:10px 0;">'; 
    echo '<div>Your Leave days are as follows: </div>'; 
    printDates($leaveDays); 
    echo '</div>'; 
} 

希望這會有所幫助。

相關問題