From ab8a99e852373086f0065bc0faf062516d1c595c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Eisenf=C3=BChrer?= Date: Fri, 29 Jan 2021 16:29:22 +0100 Subject: [PATCH] Optimmize date convert with standard DateTime format --- src/Subjects/I18n/SimpleDateConverter.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Subjects/I18n/SimpleDateConverter.php b/src/Subjects/I18n/SimpleDateConverter.php index 1d7a2502..a54d6b34 100644 --- a/src/Subjects/I18n/SimpleDateConverter.php +++ b/src/Subjects/I18n/SimpleDateConverter.php @@ -83,15 +83,18 @@ protected function getDateConverterConfiguration() */ public function convert($date, $format = 'Y-m-d H:i:s') { - // create a DateTime instance from the passed value - if ($dateTime = new \DateTime($date)) { - return $dateTime->format($format); - } // create a DateTime instance from the passed value if ($dateTime = \DateTime::createFromFormat($this->getDateConverterConfiguration()->getSourceDateFormat(), $date)) { return $dateTime->format($format); } + // Date is not in configured format? Try if is a default format + try { + return (new \DateTime($date))->format($format); + } catch( \Exception $e) { + // Catch if $date is no default date format + } + // return NULL, if the passed value is NOT a valid date return null; }