-1

我有两张表,分别称为 work_details 和 Holy_days。

Holy_days 表包含每年每个假期的一行。work_details 包含每个员工每天的工作报告的行。

我想获得所有员工的休假报告(每年和每月)。

我想显示他们每年和每月请假多少天(带日期)

计算除周六、周日和 Holy_days 表中的日期以外的休假天数。我正在使用以下代码,但这不能正常工作..任何人请帮忙。

if ($request->isPost()) {
    $sdate1 = $request->get('txt_date1','post');
    $d1=explode('/', $sdate1);
    $sdate= $d1['2'].'-'.$d1['0'].'-'.$d1['1'];
    $edate1 = $request->get('txt_date2','post');
    $d2=explode('/', $edate1);
    $edate= $d2['2'].'-'.$d2['0'].'-'.$d2['1'];
$count=dates_between($emp_id,$sdate,$edate);

echo $count;

  }


function dates_between($emp_id,$start_date, $end_date = false)
 {
     if ( !$end_date )
     {
         $end_date = date("Y-m-d");
     }

     $test_date = $start_date;
     $day_incrementer = 1;
     $count_leaves=0;
     $flag=0;
     echo "SELECT DISTINCT date FROM `work_details` WHERE  employee_id='".$emp_id."' and date between '".$start_date."' and '".$end_date."'";
 $work_res = mysql_query("SELECT DISTINCT date FROM `work_details` WHERE  employee_id='".$emp_id."' and date between '".$start_date."' and '".$end_date."'");

 do
     {
             while($row=mysql_fetch_array($work_res))
                {
                 while((date("Y-m-d",$test_date)<$row['date'])&&($flag=0))

                 {

                     if(!(date('N', strtotime(date("Y-m-d", $test_date))) >= 6))
                     {
                         echo  "<tr><td align=center class=fontclass style=color:FF0000>".date("Y-m-d", $test_date)."</td></tr>";
                           $count_leaves++;
                     }
                        $test_date = $test_date + ($day_incrementer * 60 * 60 * 24);


                 }
                 $flag=1;


                 while((date("Y-m-d",$test_date)!=$row['date']))
                    {
                   if(!(date('N', strtotime(date("Y-m-d", $test_date))) >= 6))
                   {
                      echo  "<tr><td align=center class=fontclass style=color:FF0000>".date("Y-m-d", $test_date)."</td></tr>";
                       $count_leaves++;
                   }
                       $test_date = $test_date + ($day_incrementer * 60 * 60 * 24);


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



                 }
                if(!(date('N', strtotime(date("Y-m-d", $test_date))) >= 6)&&($test_date <= $end_date))
                    {
                         echo  "<tr><td align=center class=fontclass style=color:FF0000>".date("Y-m-d", $test_date)."</td></tr>";
                         $count_leaves++;
                         $test_date = $test_date + ($day_incrementer * 60 * 60 * 24);


                    }


}
while ( $test_date <= $end_date);

return($count_leaves);
 }
4

2 回答 2

3

娜塔莎。看一下这个!你一定会感到惊讶:)

function dates_between($emp_id,$start_date, $end_date = false)
 {
    $qsdate=$start_date;
    $qedate=$end_date;
    //echo $start_date.$end_date;
     if ( !$end_date )
     {
         $end_date = date("Y-m-d");
     }

     $start_date = is_int($start_date) ? $start_date : strtotime($start_date);
     $end_date = is_int($end_date) ? $end_date : strtotime($end_date);

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

 $work_res = mysql_query("SELECT DISTINCT date FROM `work_details` WHERE  employee_id='".$emp_id."' and date between '".$qsdate."' and '".$qedate."'");


             while($row=mysql_fetch_array($work_res))
                {
                 while((date("Y-m-d",$test_date)<$row['date'])&&($flag=0))

                 {

                    if(!(date('N', strtotime(date("Y-m-d", $test_date))) >= 6))

                     {
                         echo  "<tr><td align=center class=fontclass style=color:FF0000>".date("Y-m-d", $test_date)."</td></tr>";
                           $count_leaves++;
                     }
                        $test_date = $test_date + ($day_incrementer * 60 * 60 * 24);


                 }
                 $flag=1;


                 while((date("Y-m-d",$test_date)!=$row['date']))
                    {
                 if(!(date('N', strtotime(date("Y-m-d", $test_date))) >= 6))

                   {
                      echo  "<tr><td align=center class=fontclass style=color:FF0000>".date("Y-m-d", $test_date)."</td></tr>";
                       $count_leaves++;
                   }
                       $test_date = $test_date + ($day_incrementer * 60 * 60 * 24);


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



                 }


                 while ( date("Y-m-d", $test_date) <= date("Y-m-d", $end_date))
     {
          if(!(date('N', strtotime(date("Y-m-d", $test_date))) >= 6))

                    {
                         echo  "<tr><td align=center class=fontclass style=color:FF0000>".date("Y-m-d", $test_date)."</td></tr>";
                       $count_leaves++;



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


     }


return($count_leaves);
 }
于 2012-06-12T13:24:06.143 回答
0
SELECT DISTINCT date 
FROM `work_details` 
WHERE  employee_id='".$emp_id."' 
and date between '".$start_date."' and '".$end_date."'
and date NOT IN(SELECT date from Holy_Days)

假设 Holy_Days 表中有一个日期列。

于 2012-06-11T14:08:58.007 回答