简体   繁体   English

如何将生日(电话类型日期字段类)添加到数据库并设置年龄限制?

[英]How to add birthdate (tel type datefield class) to database and set an age restriction?

As the title suggests, I am having difficulty finding a clean and elegant approach to both adding a working php function for a user's birthdate and setting an age limit to my birthdate form that I have created: 顾名思义,我很难找到一种简洁优雅的方法来为用户的生日添加一个有效的php函数,并为我创建的生日表格设置年龄限制:

<div id="date1" class="datefield">
            <input id="day" type="tel" maxlength="2" placeholder="DD"/> /
            <input id="month" type="tel" maxlength="2" placeholder="MM"/> /
            <input id="year" type="tel" maxlength="4" placeholder="YYYY" />
</div>

Here are the variables I have set up for the birthdate: 这是我为生日设置的变量:

//initializing registration variables to prevent errors
$bday = ""; //birthday
$bday = date("Y-m-d");

In my database, the birth_date type is set as just a date. 在我的数据库中,birth_date类型设置为仅一个日期。 Is my setup correct so far? 到目前为止,我的设置正确吗? I understand that there may be a need to separate the date into month, day, and year, but all I am interested in is the valid year in which the user entered if I want to include conditions regarding the different number of days each month has. 我了解可能需要将日期分为月,日和年,但是如果我想包含有关每个月的不同天数的条件,我只想输入用户输入的有效年份。

Edit: With the paragraph that was just replaced, I realized that I am making this more complex than necessary. 编辑:用刚刚被替换的段落,我意识到我正在使这一过程变得比必要的更为复杂。 All I simply would like is for a php validation function that checks whether or not an inputted year is within the desired age specifications. 我只想提供一个php验证功能,用于检查输入的年份是否在期望的年龄规格之内。 So, on first code you see, I will be removing both day and month fields and only keep the year field. 因此,在您看到的第一个代码上,我将同时删除日期和月份字段,而仅保留年份字段。

Off tangent(but still relevant), should I not even bother to prompt users to inputting their DOB and instead have the required age limit documented in the terms/privacy policy(ies)? 离切线(但仍然相关),我是否应该不费心地提示用户输入其DOB,而是在条款/隐私政策中记录所需的年龄限制? I know that even with a DOB form w/ restrictions not everyone will be willing to oblige to the rules ( ex. social net services like Facebook have 13 years as age limit but kids younger can work around that by lying; Twitter has their age limit documented rather than prompted). 我知道即使有限制的DOB表格,也不是每个人都愿意遵守规则(例如,像Facebook这样的社交网络服务的年龄限制为13岁,但是年龄较小的孩子可以通过撒谎来解决这个问题; Twitter有其年龄限制记录而不是提示)。

Much advice would be greatly appreciated, thank you. 非常感谢您提供很多建议。

you can use following code to validate date 您可以使用以下代码来验证日期

ValidationExpression="(((0|1)[1-9]|2[1-9]|3[0-1])/(0[1-9]|1[0-2])/((19|20)\\d\\d))$" ValidationExpression =“((((0 | 1)[1-9] | 2 [1-9] | 3 [0-1])/(0 [1-9] | 1 [0-2])/((19 | 20)\\ d \\ d))$“

Php has checkdate function to check if date is valid. PHP具有checkdate功能,可检查日期是否有效。 And to ensure the age you can use date_add() function to create birthday datetime object + your range. 为了确保年龄,您可以使用date_add()函数创建生日datetime对象+您的范围。 Then you compare this object if it is less than current time. 然后,如果该对象小于当前时间,则将其进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM