I am having a problem when validating a time that brings milliseconds. I'm loading records in my database with data coming from an excel. I want to validate the time.
if(!($this->validateDate($array_to_validate['control_tiempo_oficial'], 'H:i:s')) || empty($array_to_validate['control_tiempo_oficial'])){
$custom_errors['control_tiempo_oficial'] = 'El formato ingresado no es correcto.';
$error = true;
}
As you can see valid only have hours, minutes and seconds. When the data is validated it is 00:00:00,00
(contains ,). Spend validations (and what I find strange). But when 00:00:00.00
fails. I need to do is that both pass validation if they come with that data.
Use this function to validate dates and times also in these cases, but I could not get that with this format works milliseconds.
function validateDate($date, $format = 'Y-m-d H:i:s'){
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
}