Blame view

vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/DateTimeExcel/TimeParts.php 3.44 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  <?php
  
  namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
  
  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
  
  class TimeParts
  {
      /**
       * HOUROFDAY.
       *
       * Returns the hour of a time value.
       * The hour is given as an integer, ranging from 0 (12:00 A.M.) to 23 (11:00 P.M.).
       *
       * Excel Function:
       *        HOUR(timeValue)
       *
       * @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
       *                                    PHP DateTime object, or a standard time string
       *
       * @return int|string Hour
       */
      public static function hour($timeValue)
      {
          try {
              $timeValue = Functions::flattenSingleValue($timeValue);
              Helpers::nullFalseTrueToNumber($timeValue);
              if (!is_numeric($timeValue)) {
                  $timeValue = Helpers::getTimeValue($timeValue);
              }
              Helpers::validateNotNegative($timeValue);
          } catch (Exception $e) {
              return $e->getMessage();
          }
  
          // Execute function
          $timeValue = fmod($timeValue, 1);
          $timeValue = SharedDateHelper::excelToDateTimeObject($timeValue);
  
          return (int) $timeValue->format('H');
      }
  
      /**
       * MINUTE.
       *
       * Returns the minutes of a time value.
       * The minute is given as an integer, ranging from 0 to 59.
       *
       * Excel Function:
       *        MINUTE(timeValue)
       *
       * @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
       *                                    PHP DateTime object, or a standard time string
       *
       * @return int|string Minute
       */
      public static function minute($timeValue)
      {
          try {
              $timeValue = Functions::flattenSingleValue($timeValue);
              Helpers::nullFalseTrueToNumber($timeValue);
              if (!is_numeric($timeValue)) {
                  $timeValue = Helpers::getTimeValue($timeValue);
              }
              Helpers::validateNotNegative($timeValue);
          } catch (Exception $e) {
              return $e->getMessage();
          }
  
          // Execute function
          $timeValue = fmod($timeValue, 1);
          $timeValue = SharedDateHelper::excelToDateTimeObject($timeValue);
  
          return (int) $timeValue->format('i');
      }
  
      /**
       * SECOND.
       *
       * Returns the seconds of a time value.
       * The minute is given as an integer, ranging from 0 to 59.
       *
       * Excel Function:
       *        SECOND(timeValue)
       *
       * @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
       *                                    PHP DateTime object, or a standard time string
       *
       * @return int|string Second
       */
      public static function second($timeValue)
      {
          try {
              $timeValue = Functions::flattenSingleValue($timeValue);
              Helpers::nullFalseTrueToNumber($timeValue);
              if (!is_numeric($timeValue)) {
                  $timeValue = Helpers::getTimeValue($timeValue);
              }
              Helpers::validateNotNegative($timeValue);
          } catch (Exception $e) {
              return $e->getMessage();
          }
  
          // Execute function
          $timeValue = fmod($timeValue, 1);
          $timeValue = SharedDateHelper::excelToDateTimeObject($timeValue);
  
          return (int) $timeValue->format('s');
      }
  }