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\Shared\Date as SharedDateHelper;
   8  
   9  class TimeParts
  10  {
  11      use ArrayEnabled;
  12  
  13      /**
  14       * HOUROFDAY.
  15       *
  16       * Returns the hour of a time value.
  17       * The hour is given as an integer, ranging from 0 (12:00 A.M.) to 23 (11:00 P.M.).
  18       *
  19       * Excel Function:
  20       *        HOUR(timeValue)
  21       *
  22       * @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
  23       *                                    PHP DateTime object, or a standard time string
  24       *                         Or can be an array of date/time values
  25       *
  26       * @return array|int|string Hour
  27       *         If an array of numbers is passed as the argument, then the returned result will also be an array
  28       *            with the same dimensions
  29       */
  30      public static function hour($timeValue)
  31      {
  32          if (is_array($timeValue)) {
  33              return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $timeValue);
  34          }
  35  
  36          try {
  37              Helpers::nullFalseTrueToNumber($timeValue);
  38              if (!is_numeric($timeValue)) {
  39                  $timeValue = Helpers::getTimeValue($timeValue);
  40              }
  41              Helpers::validateNotNegative($timeValue);
  42          } catch (Exception $e) {
  43              return $e->getMessage();
  44          }
  45  
  46          // Execute function
  47          $timeValue = fmod($timeValue, 1);
  48          $timeValue = SharedDateHelper::excelToDateTimeObject($timeValue);
  49  
  50          return (int) $timeValue->format('H');
  51      }
  52  
  53      /**
  54       * MINUTE.
  55       *
  56       * Returns the minutes of a time value.
  57       * The minute is given as an integer, ranging from 0 to 59.
  58       *
  59       * Excel Function:
  60       *        MINUTE(timeValue)
  61       *
  62       * @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
  63       *                                    PHP DateTime object, or a standard time string
  64       *                         Or can be an array of date/time values
  65       *
  66       * @return array|int|string Minute
  67       *         If an array of numbers is passed as the argument, then the returned result will also be an array
  68       *            with the same dimensions
  69       */
  70      public static function minute($timeValue)
  71      {
  72          if (is_array($timeValue)) {
  73              return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $timeValue);
  74          }
  75  
  76          try {
  77              Helpers::nullFalseTrueToNumber($timeValue);
  78              if (!is_numeric($timeValue)) {
  79                  $timeValue = Helpers::getTimeValue($timeValue);
  80              }
  81              Helpers::validateNotNegative($timeValue);
  82          } catch (Exception $e) {
  83              return $e->getMessage();
  84          }
  85  
  86          // Execute function
  87          $timeValue = fmod($timeValue, 1);
  88          $timeValue = SharedDateHelper::excelToDateTimeObject($timeValue);
  89  
  90          return (int) $timeValue->format('i');
  91      }
  92  
  93      /**
  94       * SECOND.
  95       *
  96       * Returns the seconds of a time value.
  97       * The minute is given as an integer, ranging from 0 to 59.
  98       *
  99       * Excel Function:
 100       *        SECOND(timeValue)
 101       *
 102       * @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
 103       *                                    PHP DateTime object, or a standard time string
 104       *                         Or can be an array of date/time values
 105       *
 106       * @return array|int|string Second
 107       *         If an array of numbers is passed as the argument, then the returned result will also be an array
 108       *            with the same dimensions
 109       */
 110      public static function second($timeValue)
 111      {
 112          if (is_array($timeValue)) {
 113              return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $timeValue);
 114          }
 115  
 116          try {
 117              Helpers::nullFalseTrueToNumber($timeValue);
 118              if (!is_numeric($timeValue)) {
 119                  $timeValue = Helpers::getTimeValue($timeValue);
 120              }
 121              Helpers::validateNotNegative($timeValue);
 122          } catch (Exception $e) {
 123              return $e->getMessage();
 124          }
 125  
 126          // Execute function
 127          $timeValue = fmod($timeValue, 1);
 128          $timeValue = SharedDateHelper::excelToDateTimeObject($timeValue);
 129  
 130          return (int) $timeValue->format('s');
 131      }
 132  }