validate if a variable is of type date

1

What happens is that I'm doing a query to an IINFORMIX database and the field is of the DATETIME type with Format (year to fraction (3)). I clarify that the format I can not modify.

Currently I have this function:

function validateDate($date, $format = 'Y-m-d H:i:s'){
        $d = DateTime::createFromFormat($format, $date);
        return $d && $d->format($format) == $date;
}

and I made the validation in this way:

foreach ($value as $key1 => $value1) {
    $array[$key1] =   ( validateDate($value1, 'Y-m-d H:i:s.v' ) == 1 )? conver_date($value1,'d-m-Y H:i:s') : $value1;
}

But it does not work for me, the function works, the problem is the format to validate the date, I was reviewing the documentation but I do not find how, the date that comes from the database is this: 2017-06-28 10: 33: 31.033

Now if I execute

foreach ($value as $key1 => $value1) {
        $array[$key1] =   ( validateDate($value1, 'Y-m-d H:i:s.033' ) == 1 )? conver_date($value1,'d-m-Y H:i:s') : $value1;
    }

It works for me but only with dates that end in .033

    
asked by Andrés 29.06.2017 в 17:29
source

1 answer

1

Well I already solved it, I leave the answer in case that the same thing happens to someone, modify the function of validating if it is dated by the following:

function validateDate($date){
        $d = strtotime($date);
        return ($d>=1) ? 1 : 0;
}

And the implementation was like this:

( validateDate($value1) == 1 )? conver_date($value1,'d-m-Y H:i:s') : $value1;

Thanks anyway:)

    
answered by 29.06.2017 в 17:48