Skip to content

Commit 56e5844

Browse files
committed
Fixed calculating number of weeks in year accordingly ISO 8601
1 parent 8a8ce20 commit 56e5844

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

core/Base/Date.class.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,13 @@ public static function compare(Date $left, Date $right)
118118

119119
public static function getWeekCountInYear($year)
120120
{
121-
return date('W', mktime(0, 0, 0, 12, 31, $year));
121+
$weekCount = date('W', mktime(0, 0, 0, 12, 31, $year));
122+
123+
if ($weekCount == '01') {
124+
return date('W', mktime(0, 0, 0, 12, 24, $year));
125+
} else {
126+
return $weekCount;
127+
}
122128
}
123129

124130
public function __construct($date)

test/core/DateTest.class.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ public function testDayDifference()
4343
$right = Timestamp::create('2008-03-30 03:00:00');
4444

4545
$this->dayDifferenceTest($left, $right, 1);
46+
47+
$weekCount = Date::getWeekCountInYear(2012);
48+
$this->assertEquals($weekCount, 52, "Week count is incorrect");
49+
50+
$dateFromWeekNumberStamp = Date::makeFromWeek(5, 2012)->toStamp();
51+
$expectedDate = Date::create('2012-01-30 00:00:00')->toStamp();
52+
$this->assertEquals($dateFromWeekNumberStamp, $expectedDate, 'Creating date from week number is incorrect.');
4653

4754
// unsolved giv's case
4855
// $left = Timestamp::create('2008-10-25 03:00:00');

0 commit comments

Comments
 (0)