Converting string to Date and DateTime PHP

public function formatDate($date,$expectedFormat = 'j-M-Y', $currentFormat = 'Y-m-d'){
$date = DateTime::createFromFormat($currentFormat, $date);
return $date->format($expectedFormat);
}
echo formatDate("2020-11-20","m/d/Y","Y-m-d");   //output : 11/20/2020
echo formatDate("2020-11-20 20:57","m/d/Y h:i A","Y-m-d H:i"); output: 11/20/2020 08:57 PM
echo formatDate("2020-11-20 20:57","M j, Y","Y-m-d H:i"); output: Nov 20, 2020

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store