When I work with unconventional APIs, I sometimes get a bit of a messy return instead of a well defined date format. So I use a rather inelegant class and I readily admit that it is brutal and unconventional in principle but it does me good sometimes ^^.
class DateHelper { private const DATE_FORMATS = [ DATE_ATOM, DATE_COOKIE, DATE_RFC822, DATE_RFC850, DATE_RSS, DATE_W3C, "Y-m-d\TH:i:s.u", 'Y-m-d\TH:i:s', "Y-m-d'T'H:i:s.SSS'Z'", "Y-m-d\TH:i:s.uP", "Y-m-d\TH:i:sP", "d/m/Y H:i:s", ]; /** * @param string $inputStringDate * @return DateTime|null */ public static function createDateFromUnknownFormat(string $inputStringDate): ?DateTime { $inputStringDate = str_replace('/', '-', $inputStringDate); preg_match('/^(\d{4})\-(\d{2})-(\d{2})$/', $inputStringDate, $result); if (!empty($result)) { return DateTime::createFromFormat('Y-m-d', $inputStringDate); } preg_match('/^(\d{2})\-(\d{2})-(\d{4})$/', $inputStringDate, $result); if (!empty($result)) { return DateTime::createFromFormat('d-m-Y', $inputStringDate); } foreach (self::DATE_FORMATS as $dateFormat) { if ($dateObject = DateTime::createFromFormat($dateFormat, $inputStringDate)) { return $dateObject; } } return null; } }