diff --git a/src/PhpSpreadsheet/Calculation/DateTime.php b/src/PhpSpreadsheet/Calculation/DateTime.php index ceda521c..9f7d32f3 100644 --- a/src/PhpSpreadsheet/Calculation/DateTime.php +++ b/src/PhpSpreadsheet/Calculation/DateTime.php @@ -1242,10 +1242,14 @@ class DateTime $PHPDateObject = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($dateValue); $dayOfYear = $PHPDateObject->format('z'); $PHPDateObject->modify('-' . $dayOfYear . ' days'); - $dow = $PHPDateObject->format('w'); - $daysInFirstWeek = 7 - (($dow + (2 - $method)) % 7); - $dayOfYear -= $daysInFirstWeek; - $weekOfYear = ceil($dayOfYear / 7) + 1; + $firstDayOfFirstWeek = $PHPDateObject->format('w'); + $daysInFirstWeek = (6 - $firstDayOfFirstWeek + $method) % 7; + $interval = $dayOfYear - $daysInFirstWeek; + $weekOfYear = floor($interval / 7) + 1; + + if ($daysInFirstWeek) { + ++$weekOfYear; + } return (int) $weekOfYear; } diff --git a/tests/PhpSpreadsheetTests/Calculation/DateTimeTest.php b/tests/PhpSpreadsheetTests/Calculation/DateTimeTest.php index a7e8c3c8..d33a74ae 100644 --- a/tests/PhpSpreadsheetTests/Calculation/DateTimeTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/DateTimeTest.php @@ -140,12 +140,9 @@ class DateTimeTest extends \PHPUnit_Framework_TestCase /** * @dataProvider providerWEEKNUM - * @group fail19 */ public function testWEEKNUM() { - $this->markTestIncomplete('TODO: This test should be fixed'); - $args = func_get_args(); $expectedResult = array_pop($args); $result = call_user_func_array([DateTime::class, 'WEEKOFYEAR'], $args); diff --git a/tests/data/Calculation/DateTime/WEEKNUM.php b/tests/data/Calculation/DateTime/WEEKNUM.php index bdfa7d4a..494ef8be 100644 --- a/tests/data/Calculation/DateTime/WEEKNUM.php +++ b/tests/data/Calculation/DateTime/WEEKNUM.php @@ -6,14 +6,54 @@ return [ 1, 52, ], + [ + '2000-01-01', + 1, + 1, + ], + [ + '2000-01-02', + 1, + 2, + ], + [ + '2000-01-01', + 2, + 1, + ], + [ + '2000-01-03', + 2, + 2, + ], [ '1995-01-01', 1, 1, ], + [ + '1995-01-07', + 1, + 1, + ], + [ + '1995-01-08', + 1, + 2, + ], + [ + '1995-01-01', + 2, + 1, + ], + [ + '1995-01-02', + 2, + 2, + ], [ '3/7/1977', - 27, + 28, ], [ '3/7/1977',