Skip to content

Instantly share code, notes, and snippets.

@LucianoCharlesdeSouza
Created March 29, 2021 23:43
Show Gist options
  • Save LucianoCharlesdeSouza/b10ffcab341c07114087867b90a19381 to your computer and use it in GitHub Desktop.
Save LucianoCharlesdeSouza/b10ffcab341c07114087867b90a19381 to your computer and use it in GitHub Desktop.
<?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