How to use checkdate function if the input type is text for the year?

前端 未结 2 386
温柔的废话
温柔的废话 2020-12-22 11:50

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

相关标签:
2条回答
  • 2020-12-22 12:01

    Cast the variable to an integer:

    $year = (int) $_POST['year'];
    
    0 讨论(0)
  • 2020-12-22 12:12

    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>";
    }
    
    0 讨论(0)
提交回复
热议问题