Date input in HTML + PHP Validation

I am making a registration form and I'd like to get the birth date of each registered user.
<input class="field" type="date" name="birthday" max="9999-12-31" />
When I was making this, though, I realised that everyone can open the Inspect Element options and put any value they like. This will go to the database and just ruin the experience. I don't want to let this happen, so I am now looking for a way to do the following:
- Find a way to check if the date is valid (no matter what the format is, mm/dd/yyyy and dd/mm/yyyy will both get accepted into the database but they will be converted to yyyy-mm-dd before going there)
- Make sure the region doesn't matter (like I said, every format will get accepted but I'm a bit worried about the fact that some countries show the input field differently (For example, I stumbled across the РґРґ/РјРј/РіРіРіРі Рі. format which basically means dd/mm/yyyy y. Would that "y." at the end affect what is being stored as a variable in PHP? It just sits there, it's an abbreviation, it can't be changed by the user)
- Not let the person enter a date higher than the current one. I'm thinking of adding a simple 13-years-old age limit. How can that be done?
Before asking this question, I have been searching for a solution to all these problems for a while now. And I found out about thecheckdate()
function but from what I learnt, it accepts only the mm/dd/yyyy format which is not something I want. And it also works a little bit differently. I need to input three separate values for day, month and year. So, I went to look for alternative solutions. I stumbled across this topic: Correctly determine if date string is a valid date in that format. The solution seems to be what I'm looking for. The only problem is that it only accepts the "yyyy/mm/dd" format. So, logically, I just have to find a way to detect what the format is (where my only concern is: what if the user opens Inspect Element and changes the input type... maybe the solution to this is a simple check if $_POST['birthday'] is a type="data" element first? I'm also still worried about that "Рі./y." which appears in some regions though). After the detection, the correct function for checking whether the date is valid or not will get iterated. After all of this, the date will be simply converted to yyyy/mm/dd and the website will make one last check to see if the user is at least 13-years-old and allowed to be registered. I don't get to work with PHP very often, so any kind of help is appreciated!
Answer
Solution:
You should be able to use the PHP Date function by adding your input as string. Something like this:
$dob = date('Y-m-d', strtotime($_POST['birthday']));
This should take any date value input into the form an convert it to the format you want.
Then you could use date_diff to get the age in years and run a simple if statement:
$age = date_diff(date_create($dob), date_create('now'))->y;
if ($age <= 13)
{
...
}
Edit
It should also be able to convert any extras correctly:
$dob = date('Y-m-d', strtotime('02/01/2019 y'));
outputs: 2019-02-01
Edit 2
Based off of the accepted answer's edit here:
Looks like you can replace the '/' in a date with '-' and it should work:
$birthday = 15/05/1980;
$date = str_replace('/', '-', $birthday);
$dob = date('Y-m-d', strtotime($date));
This worked on my local server. Hopefully it helps! It also worked with the other date formats previously discussed.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: err_ossl_pem_no_start_line
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.