-1

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 the checkdate() 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!

  • Exactly, @Loko! That's one of my main concerns! I guess the solution for that is to check the format of the input field by not looking at the input but at the user's region/OS language. Not sure if that's possible with PHP though. – Plamen Dobrev Jun 13 '20 at 15:19

1 Answers1

1

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.

RShannon
  • 84
  • 4
  • Hello! Thank you for your answer! It's really helpful but I am still encountering a problem. If my date input is in the dd/mm/yyyy and the day is higher than twelve,I get the following: 1969-31-12. Any way this can be avoided by checking the format the user uses first? – Plamen Dobrev Jun 13 '20 at 21:06
  • 1
    @PlamenDobrev, Edited above to hopefully answer your question. Thanks! – RShannon Jun 13 '20 at 22:29