百科 教育 动画 游戏 博览 网址 金融 搜搜 资料
触屏版

在线: 1190   » [用户登录]   QQ   » [注册]
close ◇ 读取数据,请稍候 Loading...

.: Welcome to flymote.com [flymot.com] :.



  »

◆ 其它资料:.

工作时间计算函数

2007-03-28 22:50:43   李茂祥    原创   阅读: 5593   【本类别其它资料】 【资料库首页
上一篇 收藏 打印 收藏夹 下一篇

计算在某个时间段内的工作时间,自动排除这个时间段里面的节假日和休息日,休息日是可以设定的!
〖工作时间计算(自动排除节假日)〗

<?php
//Based on cronky's Working Days Function.
//I added modifications from heymeadows Finding Holidays function, and some other little stuff. 
//Jake Cantrell : Senior Programmer MyGov.us
//jcantrell@mygov.us
//--------------modified by LMX 20040209
//This script was designed to calculate the business days that a permit has been open
//since the application process.

include_once('kaoqing.inc'); //下面的函数用到了考勤的设定信息

function GetTimeStamp($MySqlDate)
  {
  /*
  Take a date in yyyy-mm-dd format and return it to the user in a PHP timestamp
  Robin 06/10/1999
  */
 
  $date_array = explode("-",$MySqlDate); // split the array
 
  $var_year = $date_array[0];
  $var_month = $date_array[1];
  $var_day = $date_array[2];

  $var_timestamp = mktime(0,0,0,$var_month,$var_day,$var_year);
  return($var_timestamp); // return it to the user
  }  // End function GetTimeStamp()

function ordinalDay($ord, $day, $month, $year)
  // ordinalDay returns date of the $ord $day of $month.
  // For example ordinalDay(3, 'Sun', 5, 2001) returns the
  // date of the 3rd Sunday of May (ie. Mother's Day).
  //
  // Note: $day must be the 3 char abbr. for the day, as
  //       given by date("D");
  //
  // By: heymeadows@yahoo.com

  {
  $firstOfMonth = GetTimeStamp("$year-$month-01");
  $lastOfMonth  = $firstOfMonth + date("t", $firstOfMonth) * 86400;
  $dayOccurs = 0;
 
  for ($i = $firstOfMonth; $i < $lastOfMonth ; $i += 86400)
     {
     if (date("D", $i) == $day)
        {
  $dayOccurs++;
       if ($dayOccurs == $ord)
         { $ordDay = $i; }
       }
     }
 
  $ordDay = (($ordDay - $firstOfMonth) / 86400) + 1;
if (strlen($ordDay) < 2) { $ordDay = "0$ordDay"; }
return $ordDay;
  }  // End function ordinalDay()

//注意:输入的格式为 YYYY-MM-DD 或 YYYY/MM/DD 或 数组(0->Y 1->M 2->D)
function included_Work_Dates($begdate, $enddate)
{
 global $failure,$work_days;
if (!is_array($begdate)&&$enddate == $begdate){return "0";}
else {
  if (!is_array($begdate))

   if (strpos($begdate, "-"))
      $charDelim = "-";
   else if (strpos($begdate, "/"))
      $charDelim = "/";
//get date
   $begdate = explode( $charDelim, $begdate );
   $begyear = $begdate[0];
   $begmonth = $begdate[1];
   $begday = $begdate[2];

   $enddate = explode( $charDelim, $enddate );
   $endyear = $enddate[0];
   $endmonth = $enddate[1];
   $endday = $enddate[2];
}else{
   $begyear = $begdate[0];
   $begmonth = $begdate[1];
   $begday = $begdate[2];
   $endyear = $enddate[0];
   $endmonth = $enddate[1];
   $endday = $enddate[2];
}
   if (strlen($endmonth) < 2) { $endmonth = "0$endmonth"; }
   if (strlen($endday) < 2) { $endday = "0$endday"; }
   if (strlen($begmonth) < 2) { $begmonth = "0$begmonth"; }
   if (strlen($begday) < 2) { $begday = "0$begday"; }

   $enddate1 = $endyear."-".$endmonth."-".$endday;
   $begdate1 = $begyear."-".$begmonth."-".$begday;
   $past=$begdate1;
   $workday=$holiday=0;

if (@!checkdate( $begmonth,$begday,$begyear )){
   echo "<script>alert('开始时间($begyear-$beginmonth-$begday)不合法,请重新填写!')</script>";
   $failure=1;
   return 0;
  };
if (@!checkdate( $endmonth,$endday,$endyear )){
   echo "<script>alert('结束时间($endyear-$endmonth-$endday)不合法,请重新填写!')</script>";
   $failure=1;
   return 0;
  };
if ($enddate1 < $begdate1){
   echo "<script>alert('开始时间必须早于结束时间,请重新填写!($enddate1 > $begdate1)')</script>";
   $failure=1;
   return 0;
  };
//day of begdate
$getday=mktime(0,0,0,$begmonth,$begday,$begyear);
$daynum=date("d", $getday);
$count=0;

//while loop to add workdays and holidays
while ($past != $enddate1)
{
$count++;
if ($count>1200)
{
 echo "<script>alert('总时间跨度过大(超过1200天 ),终止运行')</script>";
 break;
 //die("$past.$enddate1");
}
$month="$begmonth";
if (strlen($month) < 2) { $month = "0$month"; }
$year=$begyear;
$daynum=$daynum+01;
if (strlen($daynum) < 2) { $daynum = "0$daynum"; }

$past="$year-$month-$daynum";
$pasttime=mktime(0,0,0,$month,$daynum,$year);
$pastdayofweek=date("l",$pasttime);
if (in_array($pastdayofweek,$work_days)){$workday=$workday+1;}
//END OF FIRST UNEDITABLE SECTION


//THIS SECTION CONTAINS THE HOLIDAYS AND IS WHAT WILL NEED EDITING ONCE A YEAR IF
//YOUR HOLIDAYS CHANGE
$newyear="$year-01-01";
//$memday=ordinalDay(4, 'Mon', 5, $year);
$memorialday="$year-05-01";
$independenceday="$year-10-01";
//$labday=ordinalDay(1, 'Mon', 9, $year);
//$tks1=ordinalDay(3, 'Thu', 11, $year);
$memorialday1="$year-05-02";
$memorialday2="$year-05-03";
//$tks2=$tks1 + 1;
//$thanksgiving2="$year-11-$tks2";
$independenceday1="$year-10-02";
$independenceday2="$year-10-03";
//THIS IS THE END OF THE HOLIDAYS SECTION


//If you add extra holidays you will need to put it in here as well
if ($past==$newyear){$holiday=$holiday+1;}
//if ($past==$memorialday){if ($pastdayofweek !== "Sunday"){if ($pastdayofweek !== "Saturday"){$holiday=$holiday+1;}}}
if ($past==$memorialday){$holiday=$holiday+1;}
if ($past==$memorialday1){$holiday=$holiday+1;}
if ($past==$memorialday2){$holiday=$holiday+1;}
if ($past==$independenceday){$holiday=$holiday+1;}
if ($past==$independenceday1){$holiday=$holiday+1;}
if ($past==$independenceday2){$holiday=$holiday+1;}
//if ($past==$laborday){if ($pastdayofweek !== "Sunday"){if ($pastdayofweek !== "Saturday"){$holiday=$holiday+1;}}}
//if ($past==$thanksgiving){if ($pastdayofweek !== "Sunday"){if ($pastdayofweek !== "Saturday"){$holiday=$holiday+1;}}}
//if ($past==$thanksgiving2){if ($pastdayofweek !== "Sunday"){if ($pastdayofweek !== "Saturday"){$holiday=$holiday+1;;}}}
//if ($past==$xmas1){if ($pastdayofweek !== "Sunday"){if ($pastdayofweek !== "Saturday"){$holiday=$holiday+1;}}}
//if ($past==$xmas2){if ($pastdayofweek !== "Sunday"){if ($pastdayofweek !== "Saturday"){$holiday=$holiday+1;}}}

if ($begmonth == "02")
 {
 if($daynum == "28")
 {
 $begmonth = $begmonth + 01;
 $daynum = "00";
 }
 }

if ($begmonth == "04")
 {
 if($daynum == "30")
 {
 $begmonth = $begmonth + 01;
 $daynum = "00";
 }
 }
if ($begmonth == "06")
 {
 if($daynum == "30")
 {
 $begmonth = $begmonth + 01;
 $daynum = "00";
 }
 }
if ($begmonth == "09")
 {
 if($daynum == "30")
 {
 $begmonth = $begmonth + 01;
 $daynum = "00";
 }
 }
if ($begmonth == "11")
 {
 if($daynum == "30")
 {
 $begmonth = $begmonth + 01;
 $daynum = "00";
 }
 }

if ($daynum == "31")
 {
 $daynum = 00;
 $begmonth = $begmonth + 01;
 if ($begmonth =="13")
 {
 $begmonth = "01";
 $begyear = $begyear + 1;
 }
 }

}
//prints the elapsed workdays
$totalwork = $workday - $holiday;

//下面这种情况在工作日开始而在紧邻的休息日结束时(总共涉及两天)发生
if ($totalwork===0&&$enddate1 != $begdate1)
{
   echo "<script>alert('填写时间不合法(结束时间 $enddate1 不是工作日)\n请更改时间,重新填写!')</script>";
   $failure=1;
   return 0;

}
return $totalwork;

}
}

//注意:时间输入的格式为 HH:SS ,days用于开启计算的方式,>=1(时间跨越一天) 或 <1(当天)
function included_Work_Times($days, $start_date, $end_date){
   global $morningtime,$morningdowntime,$afternoontime,$afternoondowntime,$failure;

  if(!$days&&$start_date >= $end_date){
   echo "<script>alert('开始时间必须早于结束时间,请重新填写!')</script>";
   $failure=1;
   return 0;
  };
 
  $morningtime1=explode(":",$morningtime);//上午上班
  $morningtime1=$morningtime1[0]*60+$morningtime1[1];
  $morningdowntime1=explode(":",$morningdowntime);//上午下班
  $morningdowntime1=$morningdowntime1[0]*60+$morningdowntime1[1];
  $oneday_minutes=$morningdowntime1-$morningtime1;
  $afternoontime1=explode(":",$afternoontime);//下午上班
  $afternoontime1=$afternoontime1[0]*60+$afternoontime1[1];
  $afternoondowntime1=explode(":",$afternoondowntime);//下午下班
  $afternoondowntime1=$afternoondowntime1[0]*60+$afternoondowntime1[1];
  $oneday_minutes+=$afternoondowntime1-$afternoontime1;
  $start_date1=explode(":",$start_date);
  $end_date1=explode(":",$end_date);
  if ( $start_date1[0]>24 || $start_date1[0]< 0 )
   $start_date1[0]=0;
  if ( $start_date1[1]>59 || $start_date1[1]< 0 )
   $start_date1[1]=0;
  $start_date1=$start_date1[0]*60+$start_date1[1];
  if ( $end_date1[0]>24 || $end_date1[0]< 0 )
   $end_date1[0]=0;
  if ( $end_date1[1]>59 || $end_date1[1]< 0 )
   $end_date1[1]=0;
  $end_date1=$end_date1[0]*60+$end_date1[1];
  if($days>=1) //时间跨越一天
   {        // 结束那天的处理
    if ($end_date1 <= $morningdowntime1 && $end_date1 >= $morningtime1) //上午工作
      $minutes=$end_date1-$morningtime1;
    elseif ($end_date1 >= $afternoontime1 && $end_date1 <= $afternoondowntime1) //下午工作
      $minutes=$end_date1-$afternoontime1+$morningdowntime1-$morningtime1;
      else if($end_date1 > $afternoondowntime1)
      { //过了一整天
       $minutes=0;
       $days++;//天数加一,因为在最后入库时的天数中排除了两头
      }
      elseif($end_date1 < $afternoontime1 && $end_date1 > $morningdowntime1) //在中午休息时段
        $minutes=$morningdowntime1-$morningtime1;
      else $minutes=0; //未到上班时间
    //echo $minutes."<br>";
     // 开始那天的处理
    if ($start_date1 <= $morningdowntime1 && $start_date1 >= $morningtime1) //上午工作
      $minutes += $morningdowntime1-$start_date1+$afternoondowntime1-$afternoontime1;
    elseif ($start_date1 >= $afternoontime1 && $start_date1 <= $afternoondowntime1) //下午工作
      $minutes += $afternoondowntime1-$start_date1;
      else if($start_date1 > $afternoondowntime1)
       { //过了一整天
       $minutes+=0;
       }
       elseif($start_date1 < $afternoontime1 && $start_date1 > $morningdowntime1) //在中午休息时段
        $minutes += $afternoondowntime1-$afternoontime1;
        else
        {
         $minutes+=0; //未上班,在上午上班前开始
         $days++;//天数加一,因为在最后入库时的天数中排除了两头
        }
      //echo $minutes."<br>";exit;
  }else{  //当天
      //--------------------------------
      if($end_date1 > $afternoondowntime1) $end_date1 = $afternoondowntime1;
      elseif($end_date1 < $afternoontime1 && $end_date1 > $morningdowntime1)
            $end_date1 = $morningdowntime1;
      elseif($end_date1 < $morningtime1)
             $end_date1 = $morningtime1;
       //--------------------------------
      if($start_date1 < $morningtime1) $start_date1 = $morningtime1;
      elseif ($start_date1 > $afternoondowntime1)
             $start_date1 = $afternoondowntime1;
      elseif($start_date1 < $afternoontime1 && $start_date1 > $morningdowntime1)
             $start_date1 = $morningdowntime1;
        //---------------------------------
      if ($end_date1 <= $morningdowntime1 && $end_date1 >= $morningtime1
                       && $start_date1 >= $morningtime1) //上午工作
      $minutes=$end_date1-$start_date1;
    elseif ($end_date1 >= $afternoontime1 && $end_date1 <= $afternoondowntime1
                     && $start_date1 >= $afternoontime1) //下午工作,未跨上午
      {
       $minutes=$end_date1-$start_date1;
      }
    elseif ($end_date1 == $afternoondowntime1 && $start_date1 == $morningtime1)
    {   //过了一整天
       $minutes=0;
       $days++;//天数加一,因为在最后入库时的天数中排除了两头
    }else
        $minutes = $end_date1-$afternoontime1+$morningdowntime1-$start_date1;
  };
  return array($days,abs($minutes),$oneday_minutes);
  //return 天数,分钟数,一天工作的分钟数
  }
?>

责任编辑: admin

【最近的搜索】:

上一篇 收藏 打印 收藏夹 下一篇
CopyRight(c) 2007 - 2017 All Rights Reserved  【赣ICP备12001042号】
触屏版 | Archiver 20191118 01:13 | 简介 | 帮助 | 留言 | 关于 | 360网站安全检测平台