Skip to content

Commit

Permalink
Minus prefix should also remove leading space (#13)
Browse files Browse the repository at this point in the history
* Minus prefix should also remove space

Standard PHP strftime() also remove leading space when using the minus prefix.

* Add asserts for test '%#e' and '%-e'

---------

Co-authored-by: Fernando Herrero <[email protected]>
  • Loading branch information
maqc1 and alphp authored Jan 26, 2024
1 parent cc286d6 commit 2ee69eb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/php-8.1-strftime.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) :
case '#':
case '-':
// remove leading zeros but keep last char if also zero
return preg_replace('/^0+(?=.)/', '', $result);
return preg_replace('/^[0\s]+(?=.)/', '', $result);
}

return $result;
Expand Down
6 changes: 6 additions & 0 deletions tests/strftimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public function testDayFormats () {
$result = strftime('%e', '20220306 13:02:03');
$this->assertEquals(' 6', $result, '%e: Day of the month, with a space preceding single digits');

$result = strftime('%#e', '20220306 13:02:03');
$this->assertEquals('6', $result, '%#e: Day of the month, without leading space');

$result = strftime('%-e', '20220306 13:02:03');
$this->assertEquals('6', $result, '%-e: Day of the month, without leading space');

$result = strftime('%j', '20220306 13:02:03');
$this->assertEquals('065', $result, '%j: Day of the year, 3 digits with leading zeros');

Expand Down

0 comments on commit 2ee69eb

Please sign in to comment.