Date validation

I want to validate dates…these dates are picked from the users from a calendar.
After a search I made here and elsewhere…this method might help.
This dates are coming from the client as strings…for example:
“2015-02-18 15:00:00”
VALIDATE_INT is useless here cause it outputs FALSE when accepting input such as this above.

I cannot think other validating mechanism apart from the method I list here.

I’d try to split the string with explode() or str_split() into a date and time variable and then for each:

$his = DateTime::createFromFormat(‘H:i:s’, $time)->format(‘H:i:s’);
$ymd = DateTime::createFromFormat(‘Y-m-d’, $date)->format(‘Y-m-d’);

And then do the validation to of your choice.

This is just a rough guess, I have never tried it or had the need to try it before but it sounds plausible…

1 Like

@ThatSwede is on the right path, but you don’t have to separate the date and time, you can leave them combined.

if (DateTime::createFromFormat('Y-m-d H:i:s', $datetimeInput)->format('Y-m-d H:i:s') !== false) {

}

what about this one:

$startdate = DateTime::createFromFormat('Y-m-d H:i:s', $content['start']);
$date_errors = DateTime::getLastErrors();
if ($date_errors['warning_count'] + $date_errors['error_count'] > 0) 
    {
    echo json_encode('problem with the dates.');}

$content['start'] //this is just a the variable that holds the date string
1 Like

That looks to be okay.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.