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
6 changes: 2 additions & 4 deletions src/php-8.1-strftime.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,12 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) :
// Week
'%U' => function ($timestamp) {
// Number of weeks between date and first Sunday of year
$day = new DateTime(sprintf('%d-01 Sunday', $timestamp->format('Y')));
return sprintf('%02u', 1 + ($timestamp->format('z') - $day->format('z')) / 7);
return sprintf('%02u', intdiv((int) $timestamp->format('z') + 7 - (int) $timestamp->format('w'), 7));
},
'%V' => 'W',
'%W' => function ($timestamp) {
// Number of weeks between date and first Monday of year
$day = new DateTime(sprintf('%d-01 Monday', $timestamp->format('Y')));
return sprintf('%02u', 1 + ($timestamp->format('z') - $day->format('z')) / 7);
return sprintf('%02u', intdiv((int) $timestamp->format('z') + 7 - ((int) $timestamp->format('w') + 6) % 7, 7));
},

// Month
Expand Down
9 changes: 9 additions & 0 deletions tests/strftimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ public function testWeekFormats () {

$result = strftime('%W', '20220306 13:02:03');
$this->assertEquals('09', $result, '%W: A numeric representation of the week of the year, starting with the first Monday as the first week');

$result = strftime('%U', new DateTime('0999-12-31 00:00:00'));
$this->assertEquals('52', $result, '%U: Week number of a year before 1000');

$result = strftime('%W', new DateTime('0999-12-31 00:00:00'));
$this->assertEquals('52', $result, '%W: Week number of a year before 1000');

$result = strftime('%W', new DateTime('0001-01-01 00:00:00'));
$this->assertEquals('01', $result, '%W: Week number of a year that starts on a Monday');
}

public function testMonthFormats () {
Expand Down
Loading