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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Thumbs.db
#######################
/.vscode
/composer.lock
.phpunit.result.cache
# vim
*~
*.swp
Expand Down
4 changes: 2 additions & 2 deletions src/IntlLocaleFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class IntlLocaleFormatter extends AbstractLocaleFormatter

/** @var string[] strftime to ICU placeholders */
protected $formats = [
'%a' => 'EEE', // An abbreviated textual representation of the day Sun through Sat
'%a' => 'ccc', // An abbreviated textual representation of the day Sun through Sat
'%A' => 'EEEE', // A full textual representation of the day Sunday through Saturday
'%b' => 'MMM', // Abbreviated month name, based on the locale Jan through Dec
'%b' => 'LLL', // Abbreviated month name, based on the locale Jan through Dec
'%B' => 'MMMM', // Full month name, based on the locale January through December
'%h' => 'MMM', // Abbreviated month name, based on the locale (an alias of %b) Jan through Dec
];
Expand Down
19 changes: 12 additions & 7 deletions src/php-8.1-strftime.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
namespace PHP81_BC;

use DateTime;
use DateTimeInterface;
use DateTimeZone;
use DateTimeInterface;
use Exception;
use InvalidArgumentException;
use Locale;
use PHP81_BC\strftime\DateLocaleFormatter;
use PHP81_BC\strftime\IntlLocaleFormatter;

/**
* Locale-formatted strftime using IntlDateFormatter (PHP 8.1 compatible)
Expand All @@ -22,7 +25,9 @@
*
* @param string $format Date format
* @param integer|string|DateTime $timestamp Timestamp
* @param string|null $locale locale
* @return string
* @throws InvalidArgumentException
* @author BohwaZ <https://bohwaz.net/>
*/
function strftime (string $format, $timestamp = null, ?string $locale = null) : string {
Expand All @@ -34,15 +39,15 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) :
} catch (Exception $e) {
throw new InvalidArgumentException('$timestamp argument is neither a valid UNIX timestamp, a valid date-time string or a DateTime object.', 0, $e);
}
}

$timestamp->setTimezone(new DateTimeZone(date_default_timezone_get()));
$timestamp->setTimezone(new DateTimeZone(date_default_timezone_get()));
}

if (class_exists('\\IntlDateFormatter') && !isset($_SERVER['STRFTIME_NO_INTL'])) {
$locale = \Locale::canonicalize($locale ?? setlocale(LC_TIME, '0'));
$locale_formatter = new \PHP81_BC\strftime\IntlLocaleFormatter($locale);
$locale = Locale::canonicalize($locale ?? (Locale::getDefault() ?? setlocale(LC_TIME, '0')));
$locale_formatter = new IntlLocaleFormatter($locale);
} else {
$locale_formatter = new \PHP81_BC\strftime\DateLocaleFormatter($locale);
$locale_formatter = new DateLocaleFormatter($locale);
}

// Same order as https://www.php.net/manual/en/function.strftime.php
Expand Down Expand Up @@ -151,7 +156,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 @@ -46,6 +46,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