You are here

public static function PHPExcel_Calculation_DateTime::WORKDAY in Loft Data Grids 6.2

Same name and namespace in other branches
  1. 7.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/DateTime.php \PHPExcel_Calculation_DateTime::WORKDAY()

* WORKDAY * * Returns the date that is the indicated number of working days before or after a date (the * starting date). Working days exclude weekends and any dates identified as holidays. * Use WORKDAY to exclude weekends or holidays when you calculate invoice due dates, expected * delivery times, or the number of days of work performed. * * Excel Function: * WORKDAY(startDate,endDays[,holidays[,holiday[,...]]]) * * @access public * @category Date/Time Functions *

Parameters

mixed $startDate Excel date serial value (float), PHP date timestamp (integer),: * PHP DateTime object, or a standard date string * @param integer $endDays The number of nonweekend and nonholiday days before or after * startDate. A positive value for days yields a future date; a * negative value yields a past date. * @param mixed $holidays,... Optional series of Excel date serial value (float), PHP date * timestamp (integer), PHP DateTime object, or a standard date * strings that will be excluded from the working calendar, such * as state and federal holidays and floating holidays. * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object, * depending on the value of the ReturnDateType flag

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/DateTime.php, line 968

Class

PHPExcel_Calculation_DateTime
PHPExcel_Calculation_DateTime

Code

public static function WORKDAY($startDate, $endDays) {

  //	Retrieve the mandatory start date and days that are referenced in the function definition
  $startDate = PHPExcel_Calculation_Functions::flattenSingleValue($startDate);
  $endDays = PHPExcel_Calculation_Functions::flattenSingleValue($endDays);

  //	Flush the mandatory start date and days that are referenced in the function definition, and get the optional days
  $dateArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  array_shift($dateArgs);
  array_shift($dateArgs);
  if (is_string($startDate = self::_getDateValue($startDate)) || !is_numeric($endDays)) {
    return PHPExcel_Calculation_Functions::VALUE();
  }
  $startDate = (double) floor($startDate);
  $endDays = (int) floor($endDays);

  //	If endDays is 0, we always return startDate
  if ($endDays == 0) {
    return $startDate;
  }
  $decrementing = $endDays < 0 ? True : False;

  //	Adjust the start date if it falls over a weekend
  $startDoW = self::DAYOFWEEK($startDate, 3);
  if (self::DAYOFWEEK($startDate, 3) >= 5) {
    $startDate += $decrementing ? -$startDoW + 4 : 7 - $startDoW;
    $decrementing ? $endDays++ : $endDays--;
  }

  //	Add endDays
  $endDate = (double) $startDate + intval($endDays / 5) * 7 + $endDays % 5;

  //	Adjust the calculated end date if it falls over a weekend
  $endDoW = self::DAYOFWEEK($endDate, 3);
  if ($endDoW >= 5) {
    $endDate += $decrementing ? -$endDoW + 4 : 7 - $endDoW;
  }

  //	Test any extra holiday parameters
  if (!empty($dateArgs)) {
    $holidayCountedArray = $holidayDates = array();
    foreach ($dateArgs as $holidayDate) {
      if ($holidayDate !== NULL && trim($holidayDate) > '') {
        if (is_string($holidayDate = self::_getDateValue($holidayDate))) {
          return PHPExcel_Calculation_Functions::VALUE();
        }
        if (self::DAYOFWEEK($holidayDate, 3) < 5) {
          $holidayDates[] = $holidayDate;
        }
      }
    }
    if ($decrementing) {
      rsort($holidayDates, SORT_NUMERIC);
    }
    else {
      sort($holidayDates, SORT_NUMERIC);
    }
    foreach ($holidayDates as $holidayDate) {
      if ($decrementing) {
        if ($holidayDate <= $startDate && $holidayDate >= $endDate) {
          if (!in_array($holidayDate, $holidayCountedArray)) {
            --$endDate;
            $holidayCountedArray[] = $holidayDate;
          }
        }
      }
      else {
        if ($holidayDate >= $startDate && $holidayDate <= $endDate) {
          if (!in_array($holidayDate, $holidayCountedArray)) {
            ++$endDate;
            $holidayCountedArray[] = $holidayDate;
          }
        }
      }

      //	Adjust the calculated end date if it falls over a weekend
      $endDoW = self::DAYOFWEEK($endDate, 3);
      if ($endDoW >= 5) {
        $endDate += $decrementing ? -$endDoW + 4 : 7 - $endDoW;
      }
    }
  }
  switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
    case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL:
      return (double) $endDate;
    case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC:
      return (int) PHPExcel_Shared_Date::ExcelToPHP($endDate);
    case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT:
      return PHPExcel_Shared_Date::ExcelToPHPObject($endDate);
  }
}