Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). Thia is a

### Fixed

- Nothing yet.
- Unexpected Exception in Php DateTime. [Issue #4696](https://github.com/PHPOffice/PhpSpreadsheet/issues/4696) [Issue #917](https://github.com/PHPOffice/PhpSpreadsheet/issues/917) [PR #4697](https://github.com/PHPOffice/PhpSpreadsheet/pull/4697)

## 2025-10-25 - 5.2.0

Expand Down
7 changes: 7 additions & 0 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
use Throwable;

class Helpers
{
Expand Down Expand Up @@ -56,6 +57,12 @@ public static function getDateValue(mixed $dateValue, bool $allowBool = true): f
throw new Exception(ExcelError::NAN());
}

try {
SharedDateHelper::excelToDateTimeObject((float) $dateValue);
} catch (Throwable) {
throw new Exception(ExcelError::NAN());
}

return (float) $dateValue;
}

Expand Down
17 changes: 17 additions & 0 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/TimeParts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
use Throwable;

class TimeParts
{
Expand Down Expand Up @@ -44,6 +46,11 @@ public static function hour(mixed $timeValue): array|string|int
}

// Execute function
try {
SharedDateHelper::excelToDateTimeObject($timeValue);
} catch (Throwable) {
return ExcelError::NAN();
}
$timeValue = fmod($timeValue, 1);
$timeValue = SharedDateHelper::excelToDateTimeObject($timeValue);
SharedDateHelper::roundMicroseconds($timeValue);
Expand Down Expand Up @@ -85,6 +92,11 @@ public static function minute(mixed $timeValue): array|string|int
}

// Execute function
try {
SharedDateHelper::excelToDateTimeObject($timeValue);
} catch (Throwable) {
return ExcelError::NAN();
}
$timeValue = fmod($timeValue, 1);
$timeValue = SharedDateHelper::excelToDateTimeObject($timeValue);
SharedDateHelper::roundMicroseconds($timeValue);
Expand Down Expand Up @@ -126,6 +138,11 @@ public static function second(mixed $timeValue): array|string|int
}

// Execute function
try {
SharedDateHelper::excelToDateTimeObject($timeValue);
} catch (Throwable) {
return ExcelError::NAN();
}
$timeValue = fmod($timeValue, 1);
$timeValue = SharedDateHelper::excelToDateTimeObject($timeValue);
SharedDateHelper::roundMicroseconds($timeValue);
Expand Down
12 changes: 8 additions & 4 deletions src/PhpSpreadsheet/Shared/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Throwable;

class Date
{
Expand Down Expand Up @@ -376,15 +377,18 @@ public static function isDateTime(Cell $cell, mixed $value = null, bool $dateWit
$cell->getCalculatedValue()
);
}
$result = is_numeric($value)
&& self::isDateTimeFormat(
if (is_numeric($value)) {
$result = self::isDateTimeFormat(
$worksheet->getStyle(
$cell->getCoordinate()
)->getNumberFormat(),
$dateWithoutTimeOkay
);
} catch (Exception) {
// Result is already false, so no need to actually do anything here
/** @var float|int $value */
self::excelToDateTimeObject($value);
}
} catch (Throwable) {
$result = false;
}
$worksheet->setSelectedCells($selected);
$spreadsheet->setActiveSheetIndex($index);
Expand Down
8 changes: 7 additions & 1 deletion src/PhpSpreadsheet/Style/NumberFormat/DateFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;

use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use Throwable;

class DateFormatter
{
Expand Down Expand Up @@ -161,7 +163,11 @@ public static function format(mixed $value, string $format): string
$callback = [self::class, 'escapeQuotesCallback'];
$format = (string) preg_replace_callback('/"(.*)"/U', $callback, $format);

$dateObj = Date::excelToDateTimeObject($value);
try {
$dateObj = Date::excelToDateTimeObject($value);
} catch (Throwable) {
return StringHelper::convertToString($value);
}
// If the colon preceding minute had been quoted, as happens in
// Excel 2003 XML formats, m will not have been changed to i above.
// Change it now.
Expand Down
15 changes: 13 additions & 2 deletions src/PhpSpreadsheet/Worksheet/AutoFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule;
use Stringable;
use Throwable;

class AutoFilter implements Stringable
{
Expand Down Expand Up @@ -339,7 +340,12 @@ protected static function filterTestInDateGroupSet(mixed $cellValue, array $data
$timeZone = new DateTimeZone('UTC');

if (is_numeric($cellValue)) {
$dateTime = Date::excelToDateTimeObject((float) $cellValue, $timeZone);
try {
$dateTime = Date::excelToDateTimeObject((float) $cellValue, $timeZone);
} catch (Throwable) {
return false;
}

$cellValue = (float) $cellValue;
if ($cellValue < 1) {
// Just the time part
Expand Down Expand Up @@ -489,7 +495,12 @@ protected static function filterTestInPeriodDateSet(mixed $cellValue, array $mon
}

if (is_numeric($cellValue)) {
$dateObject = Date::excelToDateTimeObject((float) $cellValue, new DateTimeZone('UTC'));
try {
$dateObject = Date::excelToDateTimeObject((float) $cellValue, new DateTimeZone('UTC'));
} catch (Throwable) {
return false;
}

$dateValue = (int) $dateObject->format('m');
if (in_array($dateValue, $monthSet)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;

use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
use PHPUnit\Framework\TestCase;

class HelpersTest extends TestCase
{
public function testGetDateValueBadObject(): void
{
$this->expectException(CalcExp::class);
$this->expectExceptionMessage('#VALUE!');
Helpers::getDateValue($this);
}
}
103 changes: 103 additions & 0 deletions tests/PhpSpreadsheetTests/Shared/Issue4696Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Shared;

use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class Issue4696Test extends TestCase
{
private ?Spreadsheet $spreadsheet = null;

protected function tearDown(): void
{
if ($this->spreadsheet !== null) {
$this->spreadsheet->disconnectWorksheets();
$this->spreadsheet = null;
}
}

#[DataProvider('providerIsDateTime')]
public function testIsDateTime(bool $expectedResult, string $expectedFormatted, int|float|string $value): void
{
$this->spreadsheet = new Spreadsheet();
$sheet = $this->spreadsheet->getActiveSheet();
if (is_string($value) && $value[0] !== '=') {
$sheet->getCell('A1')->setValueExplicit($value, DataType::TYPE_STRING);
} else {
$sheet->getCell('A1')->setValue($value);
}
$sheet->getStyle('A1')->getNumberFormat()
->setFormatCode('yyyy-mm-dd');
self::assertSame(
$expectedResult,
Date::isDateTime($sheet->getCell('A1'))
);
self::assertSame(
$expectedFormatted,
$sheet->getCell('A1')->getFormattedValue()
);
}

public static function providerIsDateTime(): array
{
return [
'valid integer' => [true, '1903-12-31', 1461],
'valid integer stored as string' => [true, '1904-01-01', '1462'],
'valid integer stored as concatenated string' => [true, '1904-01-01', '="14"&"62"'],
'valid float' => [true, '1903-12-31', 1461.5],
'valid float stored as string' => [true, '1903-12-31', '1461.5'],
'out-of-range integer' => [false, '7000989091802000122', 7000989091802000122],
'out-of-range float' => [false, '7.000989091802E+18', 7000989091802000122.1],
'out-of-range float stored as string' => [false, '7000989091802000122.1', '7000989091802000122.1'],
'non-numeric' => [false, 'xyz', 'xyz'],
'issue 917' => [false, '5e8630b8-603c-43fe-b038-6154a3f893ab', '5e8630b8-603c-43fe-b038-6154a3f893ab'],
];
}

#[DataProvider('providerOtherFunctions')]
public function testOtherFunctions(string $function): void
{
$this->spreadsheet = new Spreadsheet();
$sheet = $this->spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue(7000989091802000122);
$sheet->getCell('A3')->setValue(39107); // 2007-01-25
$sheet->getCell('A4')->setValue(39767); // 2008-11-15
$sheet->getCell('A5')->setValue(2);
$sheet->getCell('A6')->setValue(1);
$sheet->getCell('B1')->setValue($function);
self::assertSame(
'#NUM!',
$sheet->getCell('B1')->getFormattedValue()
);
}

public static function providerOtherFunctions(): array
{
return [
['=YEAR(A1)'],
['=MONTH(A1)'],
['=DAY(A1)'],
['=DAYS(A1,A1)'],
['=DAYS360(A1,A1)'],
['=DATEDIF(A1,A1,"D")'],
['=HOUR(A1)'],
['=MINUTE(A1)'],
['=SECOND(A1)'],
['=WEEKNUM(A1)'],
['=ISOWEEKNUM(A1)'],
['=WEEKDAY(A1)'],
['=COUPDAYBS(A1,A4,A5,A6)'],
['=COUPDAYS(A3,A2,A5,A6)'],
['=COUPDAYSNC(A3,A2,A5,A6)'],
['=COUPNCD(A3,A2,A5,A6)'],
['=COUPNUM(A3,A2,A5,A6)'],
['=COUPPCD(A3,A2,A5,A6)'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function testYears(array $expectedVisible, string $rule): void
}
++$row;
$sheet->getCell("A$row")->setValue('=DATE(2041, 1, 1)'); // beyond epoch
++$row;
$sheet->getCell("A$row")->setValue(7000989091802000122); // issue 4696
++$row; // empty row at end
$this->maxRow = $maxRow = $row;
$autoFilter = $sheet->getAutoFilter();
Expand Down
22 changes: 22 additions & 0 deletions tests/PhpSpreadsheetTests/Worksheet/AutoFilter/DateGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ public function testYearMonthDayGroup(): void
self::assertEquals([6], $this->getVisible());
}

public function testIssue4696(): void
{
$year = 2011;
$sheet = $this->initSheet($year);
$sheet->getCell('A2')->setValue(7000989091802000122);
$columnFilter = $sheet->getAutoFilter()->getColumn('C');
$columnFilter->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER);
$columnFilter->createRule()
->setRule(
Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
[
'year' => $year,
'month' => 12,
'day' => 6,
]
)
->setRuleType(
Rule::AUTOFILTER_RULETYPE_DATEGROUP
);
self::assertEquals([6], $this->getVisible());
}

public function testYearMonthDayHourMinuteSecond1Group(): void
{
$year = 2011;
Expand Down