Created
March 29, 2021 23:43
-
-
Save LucianoCharlesdeSouza/b10ffcab341c07114087867b90a19381 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function validateFormat(string $date): DateTime | |
{ | |
$formats = ['dmY', 'd/m/Y', 'Ymd', 'Y-m-d']; | |
foreach ($formats as $format) { | |
$date_format = DateTime::createFromFormat($format, $date); | |
if ($date_format && $date_format->format($format) === $date) { | |
return $date_format; | |
} | |
} | |
} | |
$data = '29032021'; | |
echo $this->validateFormat($data)->format('Y-m-d'); //2021-03-29 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment