diff --git a/system/DataConverter/Cast/DatetimeCast.php b/system/DataConverter/Cast/DatetimeCast.php index 750d7c8117fe..1c1fc2dc20e3 100644 --- a/system/DataConverter/Cast/DatetimeCast.php +++ b/system/DataConverter/Cast/DatetimeCast.php @@ -29,7 +29,9 @@ public static function fromDataSource(mixed $value, array $params = []): Time self::invalidTypeValueError($value); } - return Time::parse($value); + $format = $params[0]; + + return Time::createFromFormat($format, $value); } public static function toDataSource(mixed $value, array $params = []): string diff --git a/tests/system/DataConverter/DataConverterTest.php b/tests/system/DataConverter/DataConverterTest.php index 4cdb55165fa1..1e5906bc3a36 100644 --- a/tests/system/DataConverter/DataConverterTest.php +++ b/tests/system/DataConverter/DataConverterTest.php @@ -342,6 +342,25 @@ public function testDateTimeConvertDataFromDB(): void $this->assertSame($expectedDate->getTimestamp(), $data['date']->getTimestamp()); } + public function testDateTimeConvertDataFromDBWithFormat(): void + { + $types = [ + 'id' => 'int', + 'date' => 'datetime[j-M-Y]', + ]; + $converter = $this->createDataConverter($types); + + $dbData = [ + 'id' => '1', + 'date' => '15-Feb-2009', + ]; + $data = $converter->fromDataSource($dbData); + + $this->assertInstanceOf(Time::class, $data['date']); + $expectedDate = Time::createFromFormat('j-M-Y', '15-Feb-2009'); + $this->assertSame($expectedDate->getTimestamp(), $data['date']->getTimestamp()); + } + public function testDateTimeConvertDataToDB(): void { $types = [