How to use checkdate function if the input type is

2020-02-15 09:23发布

问题:

I have the following input code that I want the user to key in his year of birth. My lecturer has asked me to use textbox for the user to put in the year of birth, date and month as dropdown list. And he also asked us to use the checkdate function to check if the user has enter the date of birth properly. ( I'm using Eclipse PDT)

This is the code for the input date of birth in php page:

<p>
<b>Date of Birth:</b>
<select size="1" name="day">
    <option value="" selected>Select Date</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18">18</option>
    <option value="19">19</option>
    <option value="20">20</option>
    <option value="21">21</option>
    <option value="22">22</option>
    <option value="23">23</option>
    <option value="24">24</option>
    <option value="25">25</option>
    <option value="26">26</option>
    <option value="27">27</option>
    <option value="28">28</option>
    <option value="29">29</option>
    <option value="30">30</option>
    <option value="31">31</option>    
</select>

<select size="1" name="month">
    <option value="" selected>Select Month</option>
    <option value="01">January</option>
    <option value="02">February</option>
    <option value="03">March</option>
    <option value="04">April</option>
    <option value="05">May</option>
    <option value="06">June</option>
    <option value="07">July</option>
    <option value="08">August</option>
    <option value="09">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option> 
</select>


<b>Year:</b>
<input type="text" name="year" size="4" maxlength="4" value="" />

So when I used checkdate function,

$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
if(checkdate($month,$day,$year) == TRUE){
//$dob is a valid date
echo "<p>Date of birth:$day $month $year</p>";
}
else {
//$dob is a not valid date
$day = null;
$month = null;
$year = null;
echo "<p><b><font color=red>Please enter a <mark>valid</mark> date of birth!</font></b></p>";

The error displayed:

Warning: checkdate () expects parameter 3 to be long,string given in C:\xamp\htdocs\1300532g\Assignment_1\registerProcess.php on line 54.

Question:

How to make the year text box to contain only number and no negative value ? I have tried to use other input type attribute but it also display error: Undefined attribute value(number)

<input type="number" name="year" size="4" maxlength="4" value="" />

回答1:

The type attribute is actually irrelevant to how PHP handles the value. Try casting it as int:

$month = $_POST['month'];
$day = $_POST['day'];
$year = (int) $_POST['year'];

if(checkdate($month,$day,$year))
{
    //$dob is a valid date
    echo "<p>Date of birth:$day $month $year</p>";
}
else 
{
    //$dob is a not valid date
    $day = null;
    $month = null;
    $year = null;
    echo "<p><b><font color=red>Please enter a <mark>valid</mark> date of birth!</font></b></p>";
}


回答2:

Cast the variable to an integer:

$year = (int) $_POST['year'];