PHP form validation for year

13,834

Solution 1

That would be sufficient for simple check;

    $input = (int)$input;
    if($input>1000 && $input<2100)
    {
      //valid
    }

Solution 2

"Sensible" is something that has to be defined by your application's business rules and/or your storage medium.

In MySQL, the DATETIME column type has a valid range of '1000-01-01 00:00:00' to '9999-12-31 23:59:59' while the TIMESTAMP has a valid range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

If you're asking the user for their birth year, then you need to be able to go back all the way to around 1900. However, if you're having them set a future appointment, then only 2009+ is valid.

Solution 3

You could just use a validation library:

http://validatious.org/learn/examples

And use min-val and max-val to limit the options. Or, even easier, just give them a combo box with valid years (html <select> tag)

Just keep in mind that valid years depends on your concept of valid. Maybe 110 years before this year could be valid if we're talking about users' ages (date(Y) - 110) but it might not be valid for some other stuff.

Solution 4

use strtotime.

Returns a timestamp on success, FALSE otherwise. Previous to PHP 5.1.0, this function would return -1 on failure.

you can do this

<?php
$year = '-1222';

$valid = true;

if (strtotime($year) === false) {
  $valid = false;
}
// more of your code
?>

Solution 5

I would check that the date has sense in your application. For example, it is the year of birth of your users, probably any date before 1900 would be impossible, and any greater than the current year has nonsense in that context

Share:
13,834
BlissC
Author by

BlissC

Part time local government officer and part time web developer. My computer's name is Diva, and my laptop's name is Jack. I love all things geeky (though I don't get on with Javascript for some reason). I work mainly in XHTML/CSS and I'm now starting to branch out further and work with PHP and MySQL. I've done bits of VBScript/ASP, and a bit of Coldfusion a while back, but what I can remember about them you can write on the back of a small postage stamp ;) and I much prefer PHP/MySQL

Updated on June 28, 2022

Comments

  • BlissC
    BlissC almost 2 years

    I'm using PHP to validate form input before the form data's submitted to a MySQL database.

    What would be the best way to validate a form input for the year only? I have a form input where users should input a date, in the form of a year only. Obviously I can check that the input's numeric and has only 4 characters, but what would be the best way to ensure that the number inputted is a sensible number for a date?

  • Jordan S. Jones
    Jordan S. Jones over 14 years
    This is exactly what I would do, except I would use PHP's built in Filtering mechanism if (filter_input(INPUT_GET /* or INPUT_POST depending */, "year", FILTER_VALIDATE_INT, array ("options" => array ("min" => 1900, "max" => 2100))) != false) ...
  • RyanY
    RyanY over 14 years
    This wouldn't work if the year was before 1970, which could be bad if the form is for year of birth.
  • Kris
    Kris over 14 years
    @Jordan S. Jones: Eeks, no matter how you indent that, it remains unreadable. Som of PHP's builtins actually do riek of "useless" and you found one.
  • Kris
    Kris over 14 years
    giving people a dropdown list (a combo box is one where you can also type...) does not ensure you will get a value predefined as valid. never trust form input, even if you have awesome client side validation.
  • RyanY
    RyanY over 14 years
    I'm 100% with your direction though...always better to use built-in functions when possible instead of reinventing the wheel.
  • BlissC
    BlissC over 14 years
    Thanks all - the answers are really useful. In this case (which I should have said) the date's the publication date of books. I guess the only proviso's that the date isn't be greater than the current year. Most years are going to be within the 20th (or current) century, but it's entirely possible that some could be earlier (though I'm not sure quite how much earlier - I guess it depends on the reading tastes of the public as the app in question is one for people to keep track of books they've read, want to read, etc. Thanks again everyone. Your responses have given me a lot to think about.