显示表格中的总休假数量

时间:2012-06-11 13:29:59

标签: php mysql date

我有两个名为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);
 }

2 个答案:

答案 0 :(得分: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);
 }

答案 1 :(得分: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表中有一个日期列。