Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   7  use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
   8  
   9  class DateParts
  10  {
  11      /**
  12       * DAYOFMONTH.
  13       *
  14       * Returns the day of the month, for a specified date. The day is given as an integer
  15       * ranging from 1 to 31.
  16       *
  17       * Excel Function:
  18       *        DAY(dateValue)
  19       *
  20       * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  21       *                                    PHP DateTime object, or a standard date string
  22       *
  23       * @return int|string Day of the month
  24       */
  25      public static function day($dateValue)
  26      {
  27          $weirdResult = self::weirdCondition($dateValue);
  28          if ($weirdResult >= 0) {
  29              return $weirdResult;
  30          }
  31  
  32          try {
  33              $dateValue = Helpers::getDateValue($dateValue);
  34          } catch (Exception $e) {
  35              return $e->getMessage();
  36          }
  37  
  38          // Execute function
  39          $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
  40  
  41          return (int) $PHPDateObject->format('j');
  42      }
  43  
  44      /**
  45       * MONTHOFYEAR.
  46       *
  47       * Returns the month of a date represented by a serial number.
  48       * The month is given as an integer, ranging from 1 (January) to 12 (December).
  49       *
  50       * Excel Function:
  51       *        MONTH(dateValue)
  52       *
  53       * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  54       *                                    PHP DateTime object, or a standard date string
  55       *
  56       * @return int|string Month of the year
  57       */
  58      public static function month($dateValue)
  59      {
  60          try {
  61              $dateValue = Helpers::getDateValue($dateValue);
  62          } catch (Exception $e) {
  63              return $e->getMessage();
  64          }
  65          if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) {
  66              return 1;
  67          }
  68  
  69          // Execute function
  70          $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
  71  
  72          return (int) $PHPDateObject->format('n');
  73      }
  74  
  75      /**
  76       * YEAR.
  77       *
  78       * Returns the year corresponding to a date.
  79       * The year is returned as an integer in the range 1900-9999.
  80       *
  81       * Excel Function:
  82       *        YEAR(dateValue)
  83       *
  84       * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  85       *                                    PHP DateTime object, or a standard date string
  86       *
  87       * @return int|string Year
  88       */
  89      public static function year($dateValue)
  90      {
  91          try {
  92              $dateValue = Helpers::getDateValue($dateValue);
  93          } catch (Exception $e) {
  94              return $e->getMessage();
  95          }
  96  
  97          if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) {
  98              return 1900;
  99          }
 100          // Execute function
 101          $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
 102  
 103          return (int) $PHPDateObject->format('Y');
 104      }
 105  
 106      /**
 107       * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
 108       *                                    PHP DateTime object, or a standard date string
 109       */
 110      private static function weirdCondition($dateValue): int
 111      {
 112          // Excel does not treat 0 consistently for DAY vs. (MONTH or YEAR)
 113          if (SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900 && Functions::getCompatibilityMode() == Functions::COMPATIBILITY_EXCEL) {
 114              if (is_bool($dateValue)) {
 115                  return (int) $dateValue;
 116              }
 117              if ($dateValue === null) {
 118                  return 0;
 119              }
 120              if (is_numeric($dateValue) && $dateValue < 1 && $dateValue >= 0) {
 121                  return 0;
 122              }
 123          }
 124  
 125          return -1;
 126      }
 127  }