Last active
April 3, 2020 17:19
-
-
Save runjak/85033fcc2afb8d3004baa52d7e94f7df 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 parse($input) { | |
$formats = array( | |
'/^\d{1,2}\.\d{1,2}\.\d{4}$/' => 'd.m.Y', | |
'/^\d{8}$/' => 'dmY' | |
); | |
foreach($formats as $pattern => $format) { | |
if(preg_match($pattern, $input)) { | |
return DateTime::createFromFormat($format, $input); | |
} | |
} | |
return null; | |
} | |
$tests = array('1.2.2001', '26081989'); | |
foreach($tests as $test) { | |
$result = parse($test); | |
$resultString = $result ? $result->format('Y-m-d') : 'null'; | |
echo "Parsing '$test' as '$resultString'.\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment