diff --git a/composer.json b/composer.json index 07cbb95..cd9978e 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "php81_bc/strftime", - "version": "0.0.6", + "version": "0.0.7", "description": "Locale-formatted strftime using IntlDateFormatter (PHP 8.1 compatible)", "license": "MIT", "authors": [ diff --git a/src/php-8.1-strftime.php b/src/php-8.1-strftime.php index 41e4010..ebdbff6 100644 --- a/src/php-8.1-strftime.php +++ b/src/php-8.1-strftime.php @@ -4,6 +4,7 @@ use DateTime; use DateTimeZone; use DateTimeInterface; + use Exception; use IntlDateFormatter; use InvalidArgumentException; @@ -29,7 +30,11 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) : if (!($timestamp instanceof DateTimeInterface)) { $timestamp = is_int($timestamp) ? '@' . $timestamp : (string) $timestamp; - $timestamp = new DateTime($timestamp); + try { + $timestamp = new DateTime($timestamp); + } 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())); diff --git a/tests/strftimeTest.php b/tests/strftimeTest.php index ed7ae9b..0c62df3 100644 --- a/tests/strftimeTest.php +++ b/tests/strftimeTest.php @@ -1,4 +1,6 @@ str_date)); $this->assertEquals('2022-03-12 01:02:03', $result); } + + public function test_exception () { + $this->expectException(InvalidArgumentException::class); + $result = strftime('%Y-%m-%d %H:%M:%S', 'InvalidArgumentException'); + } }