Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   7  
   8  class Month
   9  {
  10      use ArrayEnabled;
  11  
  12      /**
  13       * EDATE.
  14       *
  15       * Returns the serial number that represents the date that is the indicated number of months
  16       * before or after a specified date (the start_date).
  17       * Use EDATE to calculate maturity dates or due dates that fall on the same day of the month
  18       * as the date of issue.
  19       *
  20       * Excel Function:
  21       *        EDATE(dateValue,adjustmentMonths)
  22       *
  23       * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  24       *                                        PHP DateTime object, or a standard date string
  25       *                         Or can be an array of date values
  26       * @param array|int $adjustmentMonths The number of months before or after start_date.
  27       *                                        A positive value for months yields a future date;
  28       *                                        a negative value yields a past date.
  29       *                         Or can be an array of adjustment values
  30       *
  31       * @return array|mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  32       *                        depending on the value of the ReturnDateType flag
  33       *         If an array of values is passed as the argument, then the returned result will also be an array
  34       *            with the same dimensions
  35       */
  36      public static function adjust($dateValue, $adjustmentMonths)
  37      {
  38          if (is_array($dateValue) || is_array($adjustmentMonths)) {
  39              return self::evaluateArrayArguments([self::class, __FUNCTION__], $dateValue, $adjustmentMonths);
  40          }
  41  
  42          try {
  43              $dateValue = Helpers::getDateValue($dateValue, false);
  44              $adjustmentMonths = Helpers::validateNumericNull($adjustmentMonths);
  45          } catch (Exception $e) {
  46              return $e->getMessage();
  47          }
  48          $dateValue = floor($dateValue);
  49          $adjustmentMonths = floor($adjustmentMonths);
  50  
  51          // Execute function
  52          $PHPDateObject = Helpers::adjustDateByMonths($dateValue, $adjustmentMonths);
  53  
  54          return Helpers::returnIn3FormatsObject($PHPDateObject);
  55      }
  56  
  57      /**
  58       * EOMONTH.
  59       *
  60       * Returns the date value for the last day of the month that is the indicated number of months
  61       * before or after start_date.
  62       * Use EOMONTH to calculate maturity dates or due dates that fall on the last day of the month.
  63       *
  64       * Excel Function:
  65       *        EOMONTH(dateValue,adjustmentMonths)
  66       *
  67       * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  68       *                                        PHP DateTime object, or a standard date string
  69       *                         Or can be an array of date values
  70       * @param array|int $adjustmentMonths The number of months before or after start_date.
  71       *                                        A positive value for months yields a future date;
  72       *                                        a negative value yields a past date.
  73       *                         Or can be an array of adjustment values
  74       *
  75       * @return array|mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  76       *                        depending on the value of the ReturnDateType flag
  77       *         If an array of values is passed as the argument, then the returned result will also be an array
  78       *            with the same dimensions
  79       */
  80      public static function lastDay($dateValue, $adjustmentMonths)
  81      {
  82          if (is_array($dateValue) || is_array($adjustmentMonths)) {
  83              return self::evaluateArrayArguments([self::class, __FUNCTION__], $dateValue, $adjustmentMonths);
  84          }
  85  
  86          try {
  87              $dateValue = Helpers::getDateValue($dateValue, false);
  88              $adjustmentMonths = Helpers::validateNumericNull($adjustmentMonths);
  89          } catch (Exception $e) {
  90              return $e->getMessage();
  91          }
  92          $dateValue = floor($dateValue);
  93          $adjustmentMonths = floor($adjustmentMonths);
  94  
  95          // Execute function
  96          $PHPDateObject = Helpers::adjustDateByMonths($dateValue, $adjustmentMonths + 1);
  97          $adjustDays = (int) $PHPDateObject->format('d');
  98          $adjustDaysString = '-' . $adjustDays . ' days';
  99          $PHPDateObject->modify($adjustDaysString);
 100  
 101          return Helpers::returnIn3FormatsObject($PHPDateObject);
 102      }
 103  }