Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
   4  
   5  use DateTimeInterface;
   6  use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   8  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
   9  use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
  10  
  11  class Days
  12  {
  13      use ArrayEnabled;
  14  
  15      /**
  16       * DAYS.
  17       *
  18       * Returns the number of days between two dates
  19       *
  20       * Excel Function:
  21       *        DAYS(endDate, startDate)
  22       *
  23       * @param array|DateTimeInterface|float|int|string $endDate Excel date serial value (float),
  24       *           PHP date timestamp (integer), PHP DateTime object, or a standard date string
  25       *                         Or can be an array of date values
  26       * @param array|DateTimeInterface|float|int|string $startDate Excel date serial value (float),
  27       *           PHP date timestamp (integer), PHP DateTime object, or a standard date string
  28       *                         Or can be an array of date values
  29       *
  30       * @return array|int|string Number of days between start date and end date or an error
  31       *         If an array of values is passed for the $startDate or $endDays,arguments, then the returned result
  32       *            will also be an array with matching dimensions
  33       */
  34      public static function between($endDate, $startDate)
  35      {
  36          if (is_array($endDate) || is_array($startDate)) {
  37              return self::evaluateArrayArguments([self::class, __FUNCTION__], $endDate, $startDate);
  38          }
  39  
  40          try {
  41              $startDate = Helpers::getDateValue($startDate);
  42              $endDate = Helpers::getDateValue($endDate);
  43          } catch (Exception $e) {
  44              return $e->getMessage();
  45          }
  46  
  47          // Execute function
  48          $PHPStartDateObject = SharedDateHelper::excelToDateTimeObject($startDate);
  49          $PHPEndDateObject = SharedDateHelper::excelToDateTimeObject($endDate);
  50  
  51          $days = ExcelError::VALUE();
  52          $diff = $PHPStartDateObject->diff($PHPEndDateObject);
  53          if ($diff !== false && !is_bool($diff->days)) {
  54              $days = $diff->days;
  55              if ($diff->invert) {
  56                  $days = -$days;
  57              }
  58          }
  59  
  60          return $days;
  61      }
  62  }