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 PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   8  use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
   9  
  10  class DateParts
  11  {
  12      use ArrayEnabled;
  13  
  14      /**
  15       * DAYOFMONTH.
  16       *
  17       * Returns the day of the month, for a specified date. The day is given as an integer
  18       * ranging from 1 to 31.
  19       *
  20       * Excel Function:
  21       *        DAY(dateValue)
  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       *
  27       * @return array|int|string Day of the month
  28       *         If an array of numbers is passed as the argument, then the returned result will also be an array
  29       *            with the same dimensions
  30       */
  31      public static function day($dateValue)
  32      {
  33          if (is_array($dateValue)) {
  34              return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue);
  35          }
  36  
  37          $weirdResult = self::weirdCondition($dateValue);
  38          if ($weirdResult >= 0) {
  39              return $weirdResult;
  40          }
  41  
  42          try {
  43              $dateValue = Helpers::getDateValue($dateValue);
  44          } catch (Exception $e) {
  45              return $e->getMessage();
  46          }
  47  
  48          // Execute function
  49          $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
  50  
  51          return (int) $PHPDateObject->format('j');
  52      }
  53  
  54      /**
  55       * MONTHOFYEAR.
  56       *
  57       * Returns the month of a date represented by a serial number.
  58       * The month is given as an integer, ranging from 1 (January) to 12 (December).
  59       *
  60       * Excel Function:
  61       *        MONTH(dateValue)
  62       *
  63       * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  64       *                                    PHP DateTime object, or a standard date string
  65       *                         Or can be an array of date values
  66       *
  67       * @return array|int|string Month of the year
  68       *         If an array of numbers is passed as the argument, then the returned result will also be an array
  69       *            with the same dimensions
  70       */
  71      public static function month($dateValue)
  72      {
  73          if (is_array($dateValue)) {
  74              return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue);
  75          }
  76  
  77          try {
  78              $dateValue = Helpers::getDateValue($dateValue);
  79          } catch (Exception $e) {
  80              return $e->getMessage();
  81          }
  82          if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) {
  83              return 1;
  84          }
  85  
  86          // Execute function
  87          $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
  88  
  89          return (int) $PHPDateObject->format('n');
  90      }
  91  
  92      /**
  93       * YEAR.
  94       *
  95       * Returns the year corresponding to a date.
  96       * The year is returned as an integer in the range 1900-9999.
  97       *
  98       * Excel Function:
  99       *        YEAR(dateValue)
 100       *
 101       * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
 102       *                                    PHP DateTime object, or a standard date string
 103       *                         Or can be an array of date values
 104       *
 105       * @return array|int|string Year
 106       *         If an array of numbers is passed as the argument, then the returned result will also be an array
 107       *            with the same dimensions
 108       */
 109      public static function year($dateValue)
 110      {
 111          if (is_array($dateValue)) {
 112              return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue);
 113          }
 114  
 115          try {
 116              $dateValue = Helpers::getDateValue($dateValue);
 117          } catch (Exception $e) {
 118              return $e->getMessage();
 119          }
 120  
 121          if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) {
 122              return 1900;
 123          }
 124          // Execute function
 125          $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
 126  
 127          return (int) $PHPDateObject->format('Y');
 128      }
 129  
 130      /**
 131       * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
 132       *                                    PHP DateTime object, or a standard date string
 133       */
 134      private static function weirdCondition($dateValue): int
 135      {
 136          // Excel does not treat 0 consistently for DAY vs. (MONTH or YEAR)
 137          if (SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900 && Functions::getCompatibilityMode() == Functions::COMPATIBILITY_EXCEL) {
 138              if (is_bool($dateValue)) {
 139                  return (int) $dateValue;
 140              }
 141              if ($dateValue === null) {
 142                  return 0;
 143              }
 144              if (is_numeric($dateValue) && $dateValue < 1 && $dateValue >= 0) {
 145                  return 0;
 146              }
 147          }
 148  
 149          return -1;
 150      }
 151  }