You are here

public static function PHPExcel_Calculation_DateTime::DATEDIF in Loft Data Grids 7.2

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

* DATEDIF * *

Parameters

mixed $startDate Excel date serial value, PHP date/time stamp, PHP DateTime object: * or a standard date string * @param mixed $endDate Excel date serial value, PHP date/time stamp, PHP DateTime object * or a standard date string * @param string $unit * @return integer Interval between the dates

2 calls to PHPExcel_Calculation_DateTime::DATEDIF()
PHPExcel_Calculation_DateTime::YEARFRAC in vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/DateTime.php
* YEARFRAC * * Calculates the fraction of the year represented by the number of whole days between two dates * (the start_date and the end_date). * Use the YEARFRAC worksheet function to identify the proportion of a whole year's benefits…
PHPExcel_Calculation_Financial::XNPV in vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/Financial.php
* XNPV * * Returns the net present value for a schedule of cash flows that is not necessarily periodic. * To calculate the net present value for a series of cash flows that is periodic, use the NPV function. * * Excel Function: …

File

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

Class

PHPExcel_Calculation_DateTime
PHPExcel_Calculation_DateTime

Code

public static function DATEDIF($startDate = 0, $endDate = 0, $unit = 'D') {
  $startDate = PHPExcel_Calculation_Functions::flattenSingleValue($startDate);
  $endDate = PHPExcel_Calculation_Functions::flattenSingleValue($endDate);
  $unit = strtoupper(PHPExcel_Calculation_Functions::flattenSingleValue($unit));
  if (is_string($startDate = self::_getDateValue($startDate))) {
    return PHPExcel_Calculation_Functions::VALUE();
  }
  if (is_string($endDate = self::_getDateValue($endDate))) {
    return PHPExcel_Calculation_Functions::VALUE();
  }

  // Validate parameters
  if ($startDate >= $endDate) {
    return PHPExcel_Calculation_Functions::NaN();
  }

  // Execute function
  $difference = $endDate - $startDate;
  $PHPStartDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($startDate);
  $startDays = $PHPStartDateObject
    ->format('j');
  $startMonths = $PHPStartDateObject
    ->format('n');
  $startYears = $PHPStartDateObject
    ->format('Y');
  $PHPEndDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($endDate);
  $endDays = $PHPEndDateObject
    ->format('j');
  $endMonths = $PHPEndDateObject
    ->format('n');
  $endYears = $PHPEndDateObject
    ->format('Y');
  $retVal = PHPExcel_Calculation_Functions::NaN();
  switch ($unit) {
    case 'D':
      $retVal = intval($difference);
      break;
    case 'M':
      $retVal = intval($endMonths - $startMonths) + intval($endYears - $startYears) * 12;

      //	We're only interested in full months
      if ($endDays < $startDays) {
        --$retVal;
      }
      break;
    case 'Y':
      $retVal = intval($endYears - $startYears);

      //	We're only interested in full months
      if ($endMonths < $startMonths) {
        --$retVal;
      }
      elseif ($endMonths == $startMonths && $endDays < $startDays) {
        --$retVal;
      }
      break;
    case 'MD':
      if ($endDays < $startDays) {
        $retVal = $endDays;
        $PHPEndDateObject
          ->modify('-' . $endDays . ' days');
        $adjustDays = $PHPEndDateObject
          ->format('j');
        if ($adjustDays > $startDays) {
          $retVal += $adjustDays - $startDays;
        }
      }
      else {
        $retVal = $endDays - $startDays;
      }
      break;
    case 'YM':
      $retVal = intval($endMonths - $startMonths);
      if ($retVal < 0) {
        $retVal = 12 + $retVal;
      }

      //	We're only interested in full months
      if ($endDays < $startDays) {
        --$retVal;
      }
      break;
    case 'YD':
      $retVal = intval($difference);
      if ($endYears > $startYears) {
        while ($endYears > $startYears) {
          $PHPEndDateObject
            ->modify('-1 year');
          $endYears = $PHPEndDateObject
            ->format('Y');
        }
        $retVal = $PHPEndDateObject
          ->format('z') - $PHPStartDateObject
          ->format('z');
        if ($retVal < 0) {
          $retVal += 365;
        }
      }
      break;
    default:
      $retVal = PHPExcel_Calculation_Functions::NaN();
  }
  return $retVal;
}