PHP reformatting date from input field [duplicate]

I am obtaining the date from a form. Later this is used to search so it has to be the format mm-dd-yyyy but the user might type m-d-yy, m-dd-yyyy, mm/dd/yyyy,...ect. I am having trouble getting the date from the user and manipulating it get it to to form mm-dd-yyyy.

I think I worked out how to do it for the month field if the user enters only one character but I cant figure out how to replace the "/" with "-" if the user doesnt use "-" or how to prepend a 0 on the days and adding a 19 or 20 if the user only uses two digits for the year. This is what I have so far:

    $date = '7/2/2021';
    $user = strstr($date, '/', true);
        $date = "0".$date;

Read more here:

Content Attribution

This content was originally published by Daniel Burgess at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: