Skip to content

Commit

Permalink
Optimmize date convert with standard DateTime format
Browse files Browse the repository at this point in the history
  • Loading branch information
Mardl committed Jan 29, 2021
1 parent e9d2429 commit ab8a99e
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/Subjects/I18n/SimpleDateConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit ab8a99e

Please sign in to comment.