doudou1309 2015-07-12 07:43
浏览 81
已采纳

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

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:

<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. 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. 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)? 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).

Much advice would be greatly appreciated, thank you.

  • 写回答

2条回答 默认 最新

  • dougaojue8185 2015-07-12 08:03
    关注

    Php has checkdate function to check if date is valid. And to ensure the age you can use date_add() function to create birthday datetime object + your range. Then you compare this object if it is less than current time.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块