Skip to content

Commit ab8a99e

Browse files
committed
Optimmize date convert with standard DateTime format
1 parent e9d2429 commit ab8a99e

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/Subjects/I18n/SimpleDateConverter.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,18 @@ protected function getDateConverterConfiguration()
8383
*/
8484
public function convert($date, $format = 'Y-m-d H:i:s')
8585
{
86-
// create a DateTime instance from the passed value
87-
if ($dateTime = new \DateTime($date)) {
88-
return $dateTime->format($format);
89-
}
9086
// create a DateTime instance from the passed value
9187
if ($dateTime = \DateTime::createFromFormat($this->getDateConverterConfiguration()->getSourceDateFormat(), $date)) {
9288
return $dateTime->format($format);
9389
}
9490

91+
// Date is not in configured format? Try if is a default format
92+
try {
93+
return (new \DateTime($date))->format($format);
94+
} catch( \Exception $e) {
95+
// Catch if $date is no default date format
96+
}
97+
9598
// return NULL, if the passed value is NOT a valid date
9699
return null;
97100
}

0 commit comments

Comments
 (0)