Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:35:22+00:00 2026-06-18T04:35:22+00:00

How to validate day/month/year select with php? I red some topics about who to

  • 0

How to validate day/month/year select with php? I red some topics about who to validate select with 2-3 or even 5-6 select options. I would know how to validate that kind of select. But in the case of, for exemple, month, there are 32 options. Or in the case of year option, the are 60-70 options. So im interested in whats the optimal way to do select validation when you have so much options?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:35:23+00:00Added an answer on June 18, 2026 at 4:35 am

    Validating selects is fairly trivial.

    Year

    Let’s say you have a sequential range of years from 1960-2013.

    Your HTML for the selects is:

    <select name="year">
        <option value="1960">1960</option>
        ...
        <option value="2013">2013</option>
    </select>
    

    So, you know that your year has to fall in the range of 1960 to 2013:

    $year = (int) $_POST["year"]);
    
    if ($year < 1960 || $year > 2013) {
        $validationPassed = false;
    }
    

    Simple enough to validate the year.

    Month

    Now that you know what year it is, you can find out if it is a leap year. However, that is only important for a single month, so first let’s validate what month they selected:

    <select name="month">
        <option value="0">January</option>
        ...
        <option value="11">December</option>
    </select>
    

    Once again, validation of the month is easy. We know it has to be a value between 0 and 11.

    $month = (int) $_POST["month"];
    
    if ($month < 0 || $month > 11) {
        $validationPassed = false;
    }
    

    Day

    Now we know the month and the year. to validate the selected day, we need to check the following criteria:

    1. Is it a leap year?
    2. Is it the month of the year with the extra day for the leap year? (February).

    For a year to be a leap year, it has to be divisible by 400, or not divisible by 100 and divisible by 4:

    function isLeapYear($year) {
        if ($year % 400 == 0)
            return true;
        else if ($year % 100 == 0)
            return false;
        else if ($year % 4 == 0)
            return true;
    
        return false;
    }
    

    Now, we can check if it is a leap year or not. If it is a leap year, then we know we need to allow 1 extra day in February.

    We make an array of days in each month:

    $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    

    Then we get the amount of days in the current month:

    $monthDays = $daysInMonth[$month];
    

    If it is a leap year, we add a day to the total days in the month:

    if ($month == 1 && isLeapYear($year)) //February
        $monthDays++;
    

    Finally, we can validate the day

    $day = (int) $_POST["day"];
    
    if ($day < 0 || $day > $monthDays) {
        $validationPassed = false;
    }
    

    Now, all of this is basically encapsulated into the checkdate() function that was in the other answer below. So, to use checkdate() instead of all the code above, you could simply do the following:

    HTML
    (Note: checkdate() is not zero-based, so we start our days and months at 1 instead of 0.)

    <form action="myphp.php" method="post">
        <select name="day">
            <option value="1">1</option>
            ...
            <option value="31">31</option>
        </select>
    
        <select name="month">
            <option value="1">1</option>
            ...
            <option value="12">12</option>
        </select>
    
        <select name="year">
            <option value="1960">1960</option>
            ...
            <option value="2013">2013</option>
        </select>
    </form>
    

    PHP

    $day = (int) $_POST["day"];
    $month = (int) $_POST["month"];
    $year = (int) $_POST["year"];
    
    $validationPassed = checkdate($month, $day, $year);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three select option list for month,day and year <?php echo form_dropdown('dob[month]',$dates['months'],set_value('dob[month]','0'));?> <?php
Users would select their date from 3 dropdowns (day, month, year). I will combine
I'm trying to validate a date format in PHP 01/02 Day and Month. Anyway,
I would like validate the current date with question posted date if date difference
Okay, I have read about regex all day now, and still don't understand it
jQuery Validation plugin is used to validate all form data: http://docs.jquery.com/Plugins/Validation Have 3 select
Date validation is not working properly. If day = 90, month = 1 and
Suppose I have a date of birth field that I want to validate. day
I want to get the previous month total day count Code Dim period as
I validate in two ways - using spring validator for custom validation and javax.validation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.