1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel; |
4
|
|
|
|
5
|
|
|
use Datetime; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper; |
8
|
|
|
|
9
|
|
|
class TimeValue |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* TIMEVALUE. |
13
|
|
|
* |
14
|
|
|
* Returns a value that represents a particular time. |
15
|
|
|
* Use TIMEVALUE to convert a time represented by a text string to an Excel or PHP date/time stamp |
16
|
|
|
* value. |
17
|
|
|
* |
18
|
|
|
* NOTE: When used in a Cell Formula, MS Excel changes the cell format so that it matches the time |
19
|
|
|
* format of your regional settings. PhpSpreadsheet does not change cell formatting in this way. |
20
|
|
|
* |
21
|
|
|
* Excel Function: |
22
|
|
|
* TIMEVALUE(timeValue) |
23
|
|
|
* |
24
|
|
|
* @param string $timeValue A text string that represents a time in any one of the Microsoft |
25
|
|
|
* Excel time formats; for example, "6:45 PM" and "18:45" text strings |
26
|
|
|
* within quotation marks that represent time. |
27
|
|
|
* Date information in time_text is ignored. |
28
|
|
|
* |
29
|
|
|
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object, |
30
|
|
|
* depending on the value of the ReturnDateType flag |
31
|
|
|
*/ |
32
|
39 |
|
public static function fromString($timeValue) |
33
|
|
|
{ |
34
|
39 |
|
$timeValue = trim(Functions::flattenSingleValue($timeValue), '"'); |
35
|
39 |
|
$timeValue = str_replace(['/', '.'], '-', $timeValue); |
36
|
|
|
|
37
|
39 |
|
$arraySplit = preg_split('/[\/:\-\s]/', $timeValue) ?: []; |
38
|
39 |
|
if ((count($arraySplit) == 2 || count($arraySplit) == 3) && $arraySplit[0] > 24) { |
39
|
1 |
|
$arraySplit[0] = ($arraySplit[0] % 24); |
40
|
1 |
|
$timeValue = implode(':', $arraySplit); |
41
|
|
|
} |
42
|
|
|
|
43
|
39 |
|
$PHPDateArray = date_parse($timeValue); |
44
|
39 |
|
$retValue = Functions::VALUE(); |
45
|
39 |
|
if (($PHPDateArray !== false) && ($PHPDateArray['error_count'] == 0)) { |
46
|
|
|
// OpenOffice-specific code removed - it works just like Excel |
47
|
31 |
|
$excelDateValue = SharedDateHelper::formattedPHPToExcel(1900, 1, 1, $PHPDateArray['hour'], $PHPDateArray['minute'], $PHPDateArray['second']) - 1; |
48
|
|
|
|
49
|
31 |
|
$retType = Functions::getReturnDateType(); |
50
|
31 |
|
if ($retType === Functions::RETURNDATE_EXCEL) { |
51
|
29 |
|
$retValue = (float) $excelDateValue; |
52
|
2 |
|
} elseif ($retType === Functions::RETURNDATE_UNIX_TIMESTAMP) { |
53
|
1 |
|
$retValue = (int) $phpDateValue = SharedDateHelper::excelToTimestamp($excelDateValue + 25569) - 3600; |
|
|
|
|
54
|
|
|
} else { |
55
|
1 |
|
$retValue = new DateTime('1900-01-01 ' . $PHPDateArray['hour'] . ':' . $PHPDateArray['minute'] . ':' . $PHPDateArray['second']); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
39 |
|
return $retValue; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|