Introduction:
Hello guys; I have an idea that I don't know how to translate into code.
I have created a date format validator for an answer that I have given and I have also thought about implementing it in some developments that use clock data and it seems to me that the answer that I have given, although it solves the problem, is not scalable and that it can be improved:
This is the code of the validator, it only validates 2 formats:
function validateDate($date)
{
$d1 = DateTime::createFromFormat('d/m/Y h:i A', $date);
$d2 = DateTime::createFromFormat('d/m/Y H:i', $date);
return (($d1 && $d1->format('d/m/Y h:i A') === $date) || ($d2 && $d2->format('d/m/Y H:i') === $date));
}
Approach:
The problem that I pose is that instead of validating in the return
, if it meets any of the formats, I would like to read the formats from an array and, through an argument, evaluate if it meets the formats:
Format array example
$format_array =[
'A'=>'d/m/Y h:i A',
'B'=>'d/m/Y H:i',
'C'=>'d/m/Y h:i:s A',
'D'=>'d/m/Y H:i:S',
'E'=>'d-m-Y h:i A',
'F'=>'d-m-Y H:i',
'G'=>'d-m-Y h:i:s A',
'H'=>'d-m-Y H:i:S'
];
What I was hoping to do was something like the following, for the passed argument $format
:
- the format can be an array (validate formats A and B would be passed
['A','B']
) - or a string (validate formats A Only 'A' would be passed)
- o defaults to true, nothing would be passed, and true indicates that all formats should be evaluated.
function validateDate($date,$format=true) //
{
// Que se Implementaría
// esto no es funcional o escalable:
return (($d1 && $d1->format('d/m/Y h:i A') === $date) || ($d2 && $d2->format('d/m/Y H:i') === $date));
}
Research and Advances:
Leaving something like:
function validateDate($date,$format=true) //
{
//formatos escalables y seleccionables a validar.
$format_array =[
'A'=>'d/m/Y h:i A',
'B'=>'d/m/Y H:i',
'C'=>'d/m/Y h:i:s A',
'D'=>'d/m/Y H:i:S',
'E'=>'d-m-Y h:i A',
'F'=>'d-m-Y H:i',
'G'=>'d-m-Y h:i:s A',
'H'=>'d-m-Y H:i:S'
];
//Evaluación del argumento formato.
if(is_string($formmat)){
$d = DateTime::createFromFormat($format_array[$format], $date);
return ($d && $d->format($format_array[$format]) === $date);
} else if($format==true || is_array($format)){
$val= false;
//Análisis de formatos Múltiples.
//si es true como valido todo.
//si es un array validar solo los Formatos en el array.
return $val;
}
}
the idea is that instead of implementing a series of ternary parses or validators in the return
iterate and validate the formats from a resources array/object of formats.
Problem:
The problem or what I need is to be able to evaluate the case of multiple formats, I don't know what to use? It seems to me that implementing array_map
, array_walk
, would be array_reduce
the way to solve it, but I don't know how to implement them and how they can return the bolean of:
$d && $d->format($format_array[$format]) === $date
After a while I have created this script which later I will be taking it to a library/package for distribution:
class:
This class has pre-set date formats but also allows you to add additional formatting.
Additionally it can validate if the given date corresponds to some of the formats and can also convert a date to a standard format...
Tests:
output:
I thought of implementing one of the array traversal methods but a foreach does the same thing and faster...