Skip to content

Instantly share code, notes, and snippets.

@runjak
Last active April 3, 2020 17:19
Show Gist options
  • Save runjak/85033fcc2afb8d3004baa52d7e94f7df to your computer and use it in GitHub Desktop.
Save runjak/85033fcc2afb8d3004baa52d7e94f7df to your computer and use it in GitHub Desktop.
<?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