Skip to content

Commit a27056b

Browse files
author
MarkBaker
committed
Enable most of the Date/Time functions to accept array arguments
1 parent 291ea88 commit a27056b

20 files changed

+501
-32
lines changed

src/PhpSpreadsheet/Calculation/DateTime.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ public static function WORKDAY($startDate, $endDays, ...$dateArgs)
464464
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
465465
* PHP DateTime object, or a standard date string
466466
*
467-
* @return int|string Day of the month
467+
* @return array|int|string Day of the month
468468
*/
469469
public static function DAYOFMONTH($dateValue = 1)
470470
{
@@ -492,7 +492,7 @@ public static function DAYOFMONTH($dateValue = 1)
492492
* 2 Numbers 1 (Monday) through 7 (Sunday).
493493
* 3 Numbers 0 (Monday) through 6 (Sunday).
494494
*
495-
* @return int|string Day of the week value
495+
* @return array|int|string Day of the week value
496496
*/
497497
public static function WEEKDAY($dateValue = 1, $style = 1)
498498
{
@@ -704,7 +704,7 @@ public static function WEEKDAY($dateValue = 1, $style = 1)
704704
* 17 Week begins on Sunday.
705705
* 21 ISO (Jan. 4 is week 1, begins on Monday).
706706
*
707-
* @return int|string Week Number
707+
* @return array|int|string Week Number
708708
*/
709709
public static function WEEKNUM($dateValue = 1, $method = self::STARTWEEK_SUNDAY)
710710
{
@@ -727,7 +727,7 @@ public static function WEEKNUM($dateValue = 1, $method = self::STARTWEEK_SUNDAY)
727727
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
728728
* PHP DateTime object, or a standard date string
729729
*
730-
* @return int|string Week Number
730+
* @return array|int|string Week Number
731731
*/
732732
public static function ISOWEEKNUM($dateValue = 1)
733733
{
@@ -751,7 +751,7 @@ public static function ISOWEEKNUM($dateValue = 1)
751751
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
752752
* PHP DateTime object, or a standard date string
753753
*
754-
* @return int|string Month of the year
754+
* @return array|int|string Month of the year
755755
*/
756756
public static function MONTHOFYEAR($dateValue = 1)
757757
{
@@ -775,7 +775,7 @@ public static function MONTHOFYEAR($dateValue = 1)
775775
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
776776
* PHP DateTime object, or a standard date string
777777
*
778-
* @return int|string Year
778+
* @return array|int|string Year
779779
*/
780780
public static function YEAR($dateValue = 1)
781781
{
@@ -799,7 +799,7 @@ public static function YEAR($dateValue = 1)
799799
* @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
800800
* PHP DateTime object, or a standard time string
801801
*
802-
* @return int|string Hour
802+
* @return array|int|string Hour
803803
*/
804804
public static function HOUROFDAY($timeValue = 0)
805805
{
@@ -823,7 +823,7 @@ public static function HOUROFDAY($timeValue = 0)
823823
* @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
824824
* PHP DateTime object, or a standard time string
825825
*
826-
* @return int|string Minute
826+
* @return array|int|string Minute
827827
*/
828828
public static function MINUTE($timeValue = 0)
829829
{
@@ -847,7 +847,7 @@ public static function MINUTE($timeValue = 0)
847847
* @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
848848
* PHP DateTime object, or a standard time string
849849
*
850-
* @return int|string Second
850+
* @return array|int|string Second
851851
*/
852852
public static function SECOND($timeValue = 0)
853853
{

src/PhpSpreadsheet/Calculation/DateTimeExcel/DateParts.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
56
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
89

910
class DateParts
1011
{
12+
use ArrayEnabled;
13+
1114
/**
1215
* DAYOFMONTH.
1316
*
@@ -19,11 +22,18 @@ class DateParts
1922
*
2023
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
2124
* PHP DateTime object, or a standard date string
25+
* Or can be an array of date values
2226
*
23-
* @return int|string Day of the month
27+
* @return array|int|string Day of the month
28+
* If an array of numbers is passed as the argument, then the returned result will also be an array
29+
* with the same dimensions
2430
*/
2531
public static function day($dateValue)
2632
{
33+
if (is_array($dateValue)) {
34+
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue);
35+
}
36+
2737
$weirdResult = self::weirdCondition($dateValue);
2838
if ($weirdResult >= 0) {
2939
return $weirdResult;
@@ -52,11 +62,18 @@ public static function day($dateValue)
5262
*
5363
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
5464
* PHP DateTime object, or a standard date string
65+
* Or can be an array of date values
5566
*
56-
* @return int|string Month of the year
67+
* @return array|int|string Month of the year
68+
* If an array of numbers is passed as the argument, then the returned result will also be an array
69+
* with the same dimensions
5770
*/
5871
public static function month($dateValue)
5972
{
73+
if (is_array($dateValue)) {
74+
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue);
75+
}
76+
6077
try {
6178
$dateValue = Helpers::getDateValue($dateValue);
6279
} catch (Exception $e) {
@@ -83,11 +100,18 @@ public static function month($dateValue)
83100
*
84101
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
85102
* PHP DateTime object, or a standard date string
103+
* Or can be an array of date values
86104
*
87-
* @return int|string Year
105+
* @return array|int|string Year
106+
* If an array of numbers is passed as the argument, then the returned result will also be an array
107+
* with the same dimensions
88108
*/
89109
public static function year($dateValue)
90110
{
111+
if (is_array($dateValue)) {
112+
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue);
113+
}
114+
91115
try {
92116
$dateValue = Helpers::getDateValue($dateValue);
93117
} catch (Exception $e) {

src/PhpSpreadsheet/Calculation/DateTimeExcel/DateValue.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
44

55
use DateTimeImmutable;
6+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
89

910
class DateValue
1011
{
12+
use ArrayEnabled;
13+
1114
/**
1215
* DATEVALUE.
1316
*
@@ -21,20 +24,27 @@ class DateValue
2124
* Excel Function:
2225
* DATEVALUE(dateValue)
2326
*
24-
* @param string $dateValue Text that represents a date in a Microsoft Excel date format.
27+
* @param array|string $dateValue Text that represents a date in a Microsoft Excel date format.
2528
* For example, "1/30/2008" or "30-Jan-2008" are text strings within
2629
* quotation marks that represent dates. Using the default date
2730
* system in Excel for Windows, date_text must represent a date from
2831
* January 1, 1900, to December 31, 9999. Using the default date
2932
* system in Excel for the Macintosh, date_text must represent a date
3033
* from January 1, 1904, to December 31, 9999. DATEVALUE returns the
3134
* #VALUE! error value if date_text is out of this range.
35+
* Or can be an array of date values
3236
*
3337
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
3438
* depending on the value of the ReturnDateType flag
39+
* If an array of numbers is passed as the argument, then the returned result will also be an array
40+
* with the same dimensions
3541
*/
3642
public static function fromString($dateValue)
3743
{
44+
if (is_array($dateValue)) {
45+
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue);
46+
}
47+
3848
$dti = new DateTimeImmutable();
3949
$baseYear = SharedDateHelper::getExcelCalendar();
4050
$dateValue = trim(Functions::flattenSingleValue($dateValue ?? ''), '"');

src/PhpSpreadsheet/Calculation/DateTimeExcel/Time.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
44

55
use DateTime;
6+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
67
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
78
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
89
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
910

1011
class Time
1112
{
13+
use ArrayEnabled;
14+
1215
/**
1316
* TIME.
1417
*
@@ -20,23 +23,31 @@ class Time
2023
* Excel Function:
2124
* TIME(hour,minute,second)
2225
*
23-
* @param mixed $hour A number from 0 (zero) to 32767 representing the hour.
26+
* @param array|int $hour A number from 0 (zero) to 32767 representing the hour.
2427
* Any value greater than 23 will be divided by 24 and the remainder
2528
* will be treated as the hour value. For example, TIME(27,0,0) =
2629
* TIME(3,0,0) = .125 or 3:00 AM.
27-
* @param mixed $minute A number from 0 to 32767 representing the minute.
30+
* @param array|int $minute A number from 0 to 32767 representing the minute.
2831
* Any value greater than 59 will be converted to hours and minutes.
2932
* For example, TIME(0,750,0) = TIME(12,30,0) = .520833 or 12:30 PM.
30-
* @param mixed $second A number from 0 to 32767 representing the second.
33+
* @param array|int $second A number from 0 to 32767 representing the second.
3134
* Any value greater than 59 will be converted to hours, minutes,
3235
* and seconds. For example, TIME(0,0,2000) = TIME(0,33,22) = .023148
3336
* or 12:33:20 AM
37+
* If an array of numbers is passed as the argument, then the returned result will also be an array
38+
* with the same dimensions
3439
*
35-
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
40+
* @return array|mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
3641
* depending on the value of the ReturnDateType flag
42+
* If an array of numbers is passed as the argument, then the returned result will also be an array
43+
* with the same dimensions
3744
*/
3845
public static function fromHMS($hour, $minute, $second)
3946
{
47+
if (is_array($hour) || is_array($minute) || is_array($second)) {
48+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $hour, $minute, $second);
49+
}
50+
4051
try {
4152
$hour = self::toIntWithNullBool($hour);
4253
$minute = self::toIntWithNullBool($minute);

src/PhpSpreadsheet/Calculation/DateTimeExcel/TimeParts.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
56
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
89

910
class TimeParts
1011
{
12+
use ArrayEnabled;
13+
1114
/**
1215
* HOUROFDAY.
1316
*
@@ -19,11 +22,18 @@ class TimeParts
1922
*
2023
* @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
2124
* PHP DateTime object, or a standard time string
25+
* Or can be an array of date/time values
2226
*
23-
* @return int|string Hour
27+
* @return array|int|string Hour
28+
* If an array of numbers is passed as the argument, then the returned result will also be an array
29+
* with the same dimensions
2430
*/
2531
public static function hour($timeValue)
2632
{
33+
if (is_array($timeValue)) {
34+
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $timeValue);
35+
}
36+
2737
try {
2838
$timeValue = Functions::flattenSingleValue($timeValue);
2939
Helpers::nullFalseTrueToNumber($timeValue);
@@ -53,11 +63,18 @@ public static function hour($timeValue)
5363
*
5464
* @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
5565
* PHP DateTime object, or a standard time string
66+
* Or can be an array of date/time values
5667
*
57-
* @return int|string Minute
68+
* @return array|int|string Minute
69+
* If an array of numbers is passed as the argument, then the returned result will also be an array
70+
* with the same dimensions
5871
*/
5972
public static function minute($timeValue)
6073
{
74+
if (is_array($timeValue)) {
75+
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $timeValue);
76+
}
77+
6178
try {
6279
$timeValue = Functions::flattenSingleValue($timeValue);
6380
Helpers::nullFalseTrueToNumber($timeValue);
@@ -87,11 +104,18 @@ public static function minute($timeValue)
87104
*
88105
* @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
89106
* PHP DateTime object, or a standard time string
107+
* Or can be an array of date/time values
90108
*
91-
* @return int|string Second
109+
* @return array|int|string Second
110+
* If an array of numbers is passed as the argument, then the returned result will also be an array
111+
* with the same dimensions
92112
*/
93113
public static function second($timeValue)
94114
{
115+
if (is_array($timeValue)) {
116+
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $timeValue);
117+
}
118+
95119
try {
96120
$timeValue = Functions::flattenSingleValue($timeValue);
97121
Helpers::nullFalseTrueToNumber($timeValue);

src/PhpSpreadsheet/Calculation/DateTimeExcel/TimeValue.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
44

55
use Datetime;
6+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
89

910
class TimeValue
1011
{
12+
use ArrayEnabled;
13+
1114
/**
1215
* TIMEVALUE.
1316
*
@@ -21,16 +24,23 @@ class TimeValue
2124
* Excel Function:
2225
* TIMEVALUE(timeValue)
2326
*
24-
* @param string $timeValue A text string that represents a time in any one of the Microsoft
27+
* @param array|string $timeValue A text string that represents a time in any one of the Microsoft
2528
* Excel time formats; for example, "6:45 PM" and "18:45" text strings
2629
* within quotation marks that represent time.
2730
* Date information in time_text is ignored.
31+
* Or can be an array of date/time values
2832
*
2933
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
3034
* depending on the value of the ReturnDateType flag
35+
* If an array of numbers is passed as the argument, then the returned result will also be an array
36+
* with the same dimensions
3137
*/
3238
public static function fromString($timeValue)
3339
{
40+
if (is_array($timeValue)) {
41+
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $timeValue);
42+
}
43+
3444
$timeValue = trim(Functions::flattenSingleValue($timeValue ?? ''), '"');
3545
$timeValue = str_replace(['/', '.'], '-', $timeValue);
3646

0 commit comments

Comments
 (0)