DateTime::format date_create() and date_format(), this will reformat the date to your desired format before you insert into DB. Returns the formatted date string on success or FALSE on failure.
if(isset($_POST['dateOfBirth'])){
$entered = $_POST['dateOfBirth'];
$create = date_create($entered);
// If user is using a browser that does not support HTML:5 input type date.
// check to make sure the entry is indeed in the proper format that supports date_format()
if($create !== false){
$date = date_format($create, 'm-d-Y');
// Insert into DB
}else{
// Handle failure error reporting
}
}
IMPORTANT NOTE: As El_Vanja pointed out there is no way to know what the user is inputting without proper front-end formatting if the user does not have HTML5 and has JS turned off.
You could get the posted date, format and then post it back and have the user confirm their entry is correct.
For example:
user enters 03/05/2020
you could then run this through datetime and display as date_format($create, 'F-j-Y');
returning March-5-2020
and have them confirm this entry, then once confirmed, insert into DB as your originally desired format.
Validation could also be done using an explode method.
if(isset($_POST['dateOfBirth'])){
$entered = $_POST['dateOfBirth'];
$create = date_create($entered);
// If user is using a browser that does not support HTML:5 input type date.
// check to make sure the entry is indeed in the proper format that supports date_format()
if($create !== false){
$date = date_format($create, 'm-d-Y');
// explode and create variables for each section of date
list($month, $day, $year) = explode("-", $date);
// now you have each section of the datetime format in its own variable
// validation can be passed through conditionals for $month and $day
if($month <= 12){
$returnDate = date_format($create, 'F-j-Y');
// display this format to user and confirm
// echo $returnDate and simple yes/no button form here
if(isset($_POST['yes'])){
// set success css on input:focus display success to user
// update DB
}else{
// set warning css on input:focus display warning to user
// empty input form and have them reenter date.
}
}else{
// handle $month error
}
// repeat conditional for $day
}else{
// $create === false --> handle error
}
}
Another way to do this however would be to validate select fields that have specific entries of values for month. Then you can format on the back end with the returned post variables and be assured the formatting is done correct with regard to day/month.