2012-06-25 2 views
1

나는 이상한 문제가 있습니다. 나는 현재 근무일이 한달 동안이고 그 달의 총 작업 일수를 계산하는 스크립트를 가지고있다. 예를 들어 오늘 6 월 25 일에 스크립트는 "17/21 일"을 출력합니다.PHP의 근무일 계산하기 - 12 일이 월요일 인 달에는 작동하지 않습니다

이상한 점은 12 일이 월요일에 넘어지면 12 일에서 월말까지 현재 근무일이 실제 시간보다 0.0417 일 적다는 것입니다. 예를 들어, 3 월 11 일 (일요일)에는 "7/22"가 표시됩니다. 그러나 3 월 12 일에는 "7.95833333/22"가 표시됩니다.

다음은 인터넷 어딘가에있는 게시물에서 발견 한 스크립트 코드입니다. 매번이 이상한 경우를 제외하고는 완벽하게 작동합니다.

//########################################################## 
//Date Calculations 
//########################################################## 

    $datepicker = $_POST['datepicker']; 
    $dpyear = date("Y", strtotime($datepicker)); 
    $dpmonth = date("m", strtotime($datepicker)); 
    $dpday = date("d", strtotime($datepicker)); 

     $end = date('Y/m/d', mktime(0, 0, 0, $dpmonth, $dpday, $dpyear));  // seconds * minutes * hours * days 
     $first = date('Y/m/d', mktime(0, 0, 0, $dpmonth, 1, $dpyear)); 
     $firstOfYear = date('Y/m/d', mktime(0, 0, 0, 1, 1, $dpyear)); 
     $last = date('Y/m/d', mktime(0, 0, 0, $dpmonth + 1, 0, $dpyear)); 
     $firstprevyear = date('Y/m/d', mktime(0, 0, 0, $dpmonth, 1, $dpyear-1)); 
     $lastprevyear = date('Y/m/d', mktime(0, 0, 0, $dpmonth + 1, 0, $dpyear-1)); 
     $prevyearmonth = date('M\'y', mktime(0, 0, 0, $dpmonth + 1, 0, $dpyear-1)); 

     $prevyeardate = "sojd.InvoiceDate >= '$firstprevyear' and sojd.InvoiceDate <= '$lastprevyear' "; 
     $mtddaterange = "sojd.InvoiceDate >= '$first' and sojd.InvoiceDate <= '$end' "; 
     $ytddaterange = "sojd.InvoiceDate >= '$firstOfYear' and sojd.InvoiceDate <= '$end' "; 



     if (date("w", strtotime($datepicker)) == 1) {  // if today is Monday, combine weekend and Monday's numbers 
      $startDate = date('Y/m/d', mktime(0, 0, 0, $dpmonth, $dpday - 2, $dpyear)); 
      $prevdaterange = "sojd.InvoiceDate >= '$startDate' and sojd.InvoiceDate <= '$end'"; 
      $daterange = "sojd.InvoiceDate >= '$startDate' and sojd.InvoiceDate <= '$end'"; 
      $params = array($startDate, $end); 
     } else { 
      $prevdaterange = "sojd.InvoiceDate = '$end' "; 
      $daterange = "sojd.InvoiceDate = '$end'"; 
      $params = array($end); 
     } 



    $holidays=array("2012-01-02","2012-05-28","2012-07-04","2012-09-03","2012-11-22","2012-12-25"); 

function getWorkingDays($startDate,$endDate,$holidays){ 
    // do strtotime calculations just once 
    $endDate = strtotime($endDate); 
    $startDate = strtotime($startDate); 


    //The total number of days between the two dates. We compute the no. of seconds and divide it to 60*60*24 
    //We add one to inlude both dates in the interval. 
    $days = ($endDate - $startDate)/86400 + 1; 

    $no_full_weeks = floor($days/7); 
    $no_remaining_days = fmod($days, 7); 

    //It will return 1 if it's Monday,.. ,7 for Sunday 
    $the_first_day_of_week = date("N", $startDate); 
    $the_last_day_of_week = date("N", $endDate); 

    //---->The two can be equal in leap years when february has 29 days, the equal sign is added here 
    //In the first case the whole interval is within a week, in the second case the interval falls in two weeks. 
    if ($the_first_day_of_week <= $the_last_day_of_week) { 
     if ($the_first_day_of_week <= 6 && 6 <= $the_last_day_of_week) $no_remaining_days--; 
     if ($the_first_day_of_week <= 7 && 7 <= $the_last_day_of_week) $no_remaining_days--; 
    } 
    else { 
     // (edit by Tokes to fix an edge case where the start day was a Sunday 
     // and the end day was NOT a Saturday) 

     // the day of the week for start is later than the day of the week for end 
     if ($the_first_day_of_week == 7) { 
      // if the start date is a Sunday, then we definitely subtract 1 day 
      $no_remaining_days--; 

      if ($the_last_day_of_week == 6) { 
       // if the end date is a Saturday, then we subtract another day 
       $no_remaining_days--; 
      } 
     } 
     else { 
      // the start date was a Saturday (or earlier), and the end date was (Mon..Fri) 
      // so we skip an entire weekend and subtract 2 days 
      $no_remaining_days -= 2; 
     } 
    } 

    //The no. of business days is: (number of weeks between the two dates) * (5 working days) + the remainder 
//---->february in none leap years gave a remainder of 0 but still calculated weekends between first and last day, this is one way to fix it 
    $workingDays = $no_full_weeks * 5; 
    if ($no_remaining_days > 0) 
    { 
     $workingDays += $no_remaining_days; 
    } 

    //We subtract the holidays 
    foreach($holidays as $holiday){ 
     $time_stamp=strtotime($holiday); 
     //If the holiday doesn't fall in weekend 
     if ($startDate <= $time_stamp && $time_stamp <= $endDate && date("N",$time_stamp) != 6 && date("N",$time_stamp) != 7) 
      $workingDays--; 
    } 

    return $workingDays; 
} 
    $currSalesDay = getWorkingDays($first,$end,$holidays); 
    $totalSalesDay = round(getWorkingDays($first,$last,$holidays),0); 
+0

지금까지 어떤 디버깅 작업을 수행 했습니까? –

+0

지금까지 코드를 쳐다 보는 것 외에는 무엇이 잘못 될지 전혀 모릅니다. 아래의 Scott의 제안을 살펴보고 있습니다. – BlueCaret

답변

5

/ 86400 작업을 수행하는 경우 두 번째 문제가 될 것입니다. http://en.wikipedia.org/wiki/Leap_second

strtotime('+1 day', $timestamp) 또는 DateTime 클래스를 사용하여 매일 이동하거나 날짜를 비교하는 것이 좋습니다.

적어도 하루 종일 처리하는 경우 결과를 반올림하십시오.

+0

strtotime이 작동하지 않아 결과가 반올림되었습니다. 문제가 어디에 있었는지 지적 해 주셔서 감사합니다! 나는 몰랐다. 감사! – BlueCaret

+0

반갑습니다. 시간 코드는 미친 짓이야. –

관련 문제