Php 如何从给定日期范围中获取工作日缺勤天数?

Php 如何从给定日期范围中获取工作日缺勤天数?,php,Php,这是我用来获取员工在给定日期范围内休假天数列表的函数。如果叶子是一个或两个也可以,但它太复杂了,检索结果需要很多时间,因此会导致超时错误!有什么帮助吗 这就是功能: function dates_between($emp_id, $start_date, $end_date) { $day_incrementer = 1; $count_leaves = 0; $flag = 0; // Getting the days from DB where the

这是我用来获取员工在给定日期范围内休假天数列表的函数。如果叶子是一个或两个也可以,但它太复杂了,检索结果需要很多时间,因此会导致超时错误!有什么帮助吗

这就是功能:

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);
 }
函数日期介于($emp\u id、$start\u date、$end\u date)之间
{
$day_incrementer=1;
$count_leaves=0;
$flag=0;
//从DB获取员工“28”在给定日期范围内工作的天数
$work_res=mysql_query(“从'work_details'中选择不同的日期,其中employee_id='28',日期介于'2012-02-01'和'2012-02-29'之间”);
做{
而($row=mysql\u fetch\u数组($work\u res))
{
而((日期($Y-m-d,$start_date)<$row['date'])&($flag=0))
//循环查找小于表日期的startdate!如果表日期(出勤)从3开始,如果不是周末,我们需要打印1,2页
{
如果(!(日期('N',标准时间(日期('Y-m-d',$start_date))>=6))
{    
//检查周末,仅打印工作日
回显日期(“Y-m-d”,开始日期)。“\n”;
$count_leaves++;
}
$start_date=$start_date+($day_incrementer*60*60*24);
}
$flag=1;
而((日期($Y-m-d,$start_date)!=$row['date']))
//循环打印$start_date,该日期不等于表日期
{
如果(!(日期('N',标准时间(日期('Y-m-d',$start_date))>=6))
{
回显日期(“Y-m-d”,开始日期)。“\n”;
$count_leaves++;
}
$$start\u date=$start\u date+($day\u incrementer*60*60*24);
}
$start_date=$start_date+($day_incrementer*60*60*24);
}
//循环打印$start_date,如果tabledate以28结束,则在tabledate之后剩余,打印其余日期29,30

如果(!(date('N',strottime(date('Y-m-d',$start_date))>=6)和($start_date),我注意到你在别处也问了类似的问题(http://stackoverflow.com/questions/10898293/how-to-get-days-of-leave-taken-in-a-given-month)。现在,我尝试深入了解您的代码,以基本了解您正在尝试的内容。如果我的回答不完全符合您的要求,请原谅,因为不容易理解他人的想法。基本上,我所做的是准备一个示例代码,满足您的要求。此代码包含特定员工工作的一系列日期一个给定的月份和年份。然后它继续获取该给定月份、该年份中可用的所有工作日期。两个数组的差值给出了工人缺席的日期(由于休假或擅离职守)。公共假日尚未计算在内,但当然,您可以轻松修改代码以添加该值。如果您将公共假日日期保存在另一个数组中,并将其与第一个结果进行差异,则最终数组将为您提供所需的内容

现在,请注意一点警告,此代码是基本代码,如果两个数组的日期格式不完全相同,则数组差异将使您失败。我个人将编写自己的比较回调函数来比较各个日期,并将其传递到数组_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>';
}

你是如何存储休假日的?你的代码格式太糟糕了。请将其格式化,使其实际上处于可读状态。你是否将考勤存储在表中?你可以在表中检查缺席情况。我不太理解你的代码,但只想指出,如果你有夏令时,60 x 60 x 24是一个非常糟糕的主意。时刻时钟变为夏季,上面的代码将进入一个无休止的循环。请不要为新代码使用
mysql.*
函数。它们不再被维护,社区已经开始运行。请参阅?相反,您应该了解并使用or。如果您不能决定,将帮助选择。如果您愿意学习。