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 Datetime;
   6  use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   8  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
   9  use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
  10  
  11  class TimeValue
  12  {
  13      use ArrayEnabled;
  14  
  15      /**
  16       * TIMEVALUE.
  17       *
  18       * Returns a value that represents a particular time.
  19       * Use TIMEVALUE to convert a time represented by a text string to an Excel or PHP date/time stamp
  20       * value.
  21       *
  22       * NOTE: When used in a Cell Formula, MS Excel changes the cell format so that it matches the time
  23       * format of your regional settings. PhpSpreadsheet does not change cell formatting in this way.
  24       *
  25       * Excel Function:
  26       *        TIMEVALUE(timeValue)
  27       *
  28       * @param null|array|string $timeValue A text string that represents a time in any one of the Microsoft
  29       *                                    Excel time formats; for example, "6:45 PM" and "18:45" text strings
  30       *                                    within quotation marks that represent time.
  31       *                                    Date information in time_text is ignored.
  32       *                         Or can be an array of date/time values
  33       *
  34       * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  35       *                        depending on the value of the ReturnDateType flag
  36       *         If an array of numbers is passed as the argument, then the returned result will also be an array
  37       *            with the same dimensions
  38       */
  39      public static function fromString($timeValue)
  40      {
  41          if (is_array($timeValue)) {
  42              return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $timeValue);
  43          }
  44  
  45          // try to parse as time iff there is at least one digit
  46          if (is_string($timeValue) && preg_match('/\\d/', $timeValue) !== 1) {
  47              return ExcelError::VALUE();
  48          }
  49  
  50          $timeValue = trim($timeValue ?? '', '"');
  51          $timeValue = str_replace(['/', '.'], '-', $timeValue);
  52  
  53          $arraySplit = preg_split('/[\/:\-\s]/', $timeValue) ?: [];
  54          if ((count($arraySplit) == 2 || count($arraySplit) == 3) && $arraySplit[0] > 24) {
  55              $arraySplit[0] = ($arraySplit[0] % 24);
  56              $timeValue = implode(':', $arraySplit);
  57          }
  58  
  59          $PHPDateArray = Helpers::dateParse($timeValue);
  60          $retValue = ExcelError::VALUE();
  61          if (Helpers::dateParseSucceeded($PHPDateArray)) {
  62              /** @var int */
  63              $hour = $PHPDateArray['hour'];
  64              /** @var int */
  65              $minute = $PHPDateArray['minute'];
  66              /** @var int */
  67              $second = $PHPDateArray['second'];
  68              // OpenOffice-specific code removed - it works just like Excel
  69              $excelDateValue = SharedDateHelper::formattedPHPToExcel(1900, 1, 1, $hour, $minute, $second) - 1;
  70  
  71              $retType = Functions::getReturnDateType();
  72              if ($retType === Functions::RETURNDATE_EXCEL) {
  73                  $retValue = (float) $excelDateValue;
  74              } elseif ($retType === Functions::RETURNDATE_UNIX_TIMESTAMP) {
  75                  $retValue = (int) SharedDateHelper::excelToTimestamp($excelDateValue + 25569) - 3600;
  76              } else {
  77                  $retValue = new DateTime('1900-01-01 ' . $PHPDateArray['hour'] . ':' . $PHPDateArray['minute'] . ':' . $PHPDateArray['second']);
  78              }
  79          }
  80  
  81          return $retValue;
  82      }
  83  }