简体   繁体   English

html和php日期输入字段

[英]html and php date input field

I am having issues trying to figure out how, to create an html input box for the date like this: 我在尝试弄清楚如何为这样的日期创建html输入框时遇到问题:

YYYY-MM-DD YYYY-MM-DD

I was wondering if there is a better method of doing this than me manually creating something like this, and then using the - separators to separate the YYYY-MM-DD. 我想知道是否有比手动创建类似的方法然后使用-分隔符分隔YYYY-MM-DD更好的方法。

I will be using regex to do some validation after the user has entered the date of birth to make sure that it is greater than 21. 在用户输入出生日期之后,我将使用regex进行验证,以确保它大于21。

<label for="dob">Age (YYYY-MM-DD)</label>
<input id="dateofbirth" type="text" name="age" maxlength="10"/><span>*</span><br />

Here is my regex for the date that will be inside function checkdate on the php side: 这是我的正则表达式的日期,该日期将在php端的函数checkdate中:

return preg_match('/(\d{4})-(\d{2})-(\d{2})/', $date); 

Set date such that it will only take input from users over 21. Currently this does not. 设置日期,使其仅接受21岁以上用户的输入。当前不这样做。
$date = '1974-12-03'; $ date ='1974-12-03';

I would simply use a jquery date picker . 我只会使用一个jQuery日期选择器

This way you don't have to worry about the input format and you can easily get the date entered in a single format to perform validation in javascript or PHP (if you pass the date back to the server). 这样,您不必担心输入格式,并且可以轻松获取以单一格式输入的日期,以便在javascript或PHP中执行验证(如果您将日期传递回服务器)。


if($age = strtotime($date)){ 
 if( ( date('Y',time()) - date('Y',$age ) >= 21 ){
  return true; 
 }else{ 
  return false; } 
}else{ 
 echo 'There seems to be something wrong with your age input'; 
}

That will verify that the user is over/equal the age of 21 这将验证用户是否超过/等于21岁

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

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