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 src/php-8.1-strftime.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) :
// Year
'%C' => function ($timestamp) {
// Century (-1): 19 for 20th century
return floor($timestamp->format('Y') / 100);
return sprintf('%02d', intdiv((int) $timestamp->format('Y'), 100));
},
'%g' => function ($timestamp) {
return substr($timestamp->format('o'), -2);
Expand Down
15 changes: 15 additions & 0 deletions tests/strftimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ public function testYearFormats () {
$result = strftime('%C', '20220306 13:02:03');
$this->assertEquals('20', $result, '%C: Two digit representation of the century (year divided by 100, truncated to an integer)');

$result = strftime('%C', new DateTime('0001-01-01 00:00:00'));
$this->assertEquals('00', $result, '%C: Two digit representation of the century, padded with a leading zero');

$result = strftime('%C', new DateTime('0999-12-31 00:00:00'));
$this->assertEquals('09', $result, '%C: Two digit representation of the century, padded with a leading zero');

$result = strftime('%C', new DateTime('1000-01-01 00:00:00'));
$this->assertEquals('10', $result, '%C: Two digit representation of the century (year divided by 100, truncated to an integer)');

$result = strftime('%-C', new DateTime('0999-12-31 00:00:00'));
$this->assertEquals('9', $result, '%-C: Century without the leading zero');

$result = strftime('%_C', new DateTime('0999-12-31 00:00:00'));
$this->assertEquals(' 9', $result, '%_C: Century padded with a leading space');

$result = strftime('%g', '20220306 13:02:03');
$this->assertEquals('22', $result, '%g: Two digit representation of the year going by ISO-8601:1988 standards (see %V)');

Expand Down
Loading