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] [Versions 401 and 402] [Versions 401 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 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          $timeValue = trim($timeValue ?? '', '"');
  46          $timeValue = str_replace(['/', '.'], '-', $timeValue);
  47  
  48          $arraySplit = preg_split('/[\/:\-\s]/', $timeValue) ?: [];
  49          if ((count($arraySplit) == 2 || count($arraySplit) == 3) && $arraySplit[0] > 24) {
  50              $arraySplit[0] = ($arraySplit[0] % 24);
  51              $timeValue = implode(':', $arraySplit);
  52          }
  53  
  54          $PHPDateArray = Helpers::dateParse($timeValue);
  55          $retValue = ExcelError::VALUE();
  56          if (Helpers::dateParseSucceeded($PHPDateArray)) {
  57              /** @var int */
  58              $hour = $PHPDateArray['hour'];
  59              /** @var int */
  60              $minute = $PHPDateArray['minute'];
  61              /** @var int */
  62              $second = $PHPDateArray['second'];
  63              // OpenOffice-specific code removed - it works just like Excel
  64              $excelDateValue = SharedDateHelper::formattedPHPToExcel(1900, 1, 1, $hour, $minute, $second) - 1;
  65  
  66              $retType = Functions::getReturnDateType();
  67              if ($retType === Functions::RETURNDATE_EXCEL) {
  68                  $retValue = (float) $excelDateValue;
  69              } elseif ($retType === Functions::RETURNDATE_UNIX_TIMESTAMP) {
  70                  $retValue = (int) $phpDateValue = SharedDateHelper::excelToTimestamp($excelDateValue + 25569) - 3600;
  71              } else {
  72                  $retValue = new DateTime('1900-01-01 ' . $PHPDateArray['hour'] . ':' . $PHPDateArray['minute'] . ':' . $PHPDateArray['second']);
  73              }
  74          }
  75  
  76          return $retValue;
  77      }
  78  }