From aa9e2edbe80123c7f4d69730c279b1c6ee0b7eb4 Mon Sep 17 00:00:00 2001 From: Sami Puro Date: Mon, 23 Nov 2026 13:55:31 +0200 Subject: [PATCH] Pad the century in %C to two digits --- src/php-8.1-strftime.php | 2 +- tests/strftimeTest.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/php-8.1-strftime.php b/src/php-8.1-strftime.php index ae72f7a..0846c35 100644 --- a/src/php-8.1-strftime.php +++ b/src/php-8.1-strftime.php @@ -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); diff --git a/tests/strftimeTest.php b/tests/strftimeTest.php index f012bb7..c1e19fb 100644 --- a/tests/strftimeTest.php +++ b/tests/strftimeTest.php @@ -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)');