views:

194

answers:

5

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?

+1  A: 

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
?>
Gabriel Sosa
This wouldn't work if the year was before 1970, which could be bad if the form is for year of birth.
Phage
@Phage you are right. seems I didn't think this correctly
Gabriel Sosa
I'm 100% with your direction though...always better to use built-in functions when possible instead of reinventing the wheel.
Phage
+1  A: 

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

rossoft
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.
NeonBlue Bliss
+2  A: 

"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.

Jonathan Campbell
+3  A: 

That would be sufficient for simple check;

    $input = (int)$input;
    if($input>1000 && $input<2100)
    {
      //valid
    }
mth
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) ...`
Jordan S. Jones
@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
+1  A: 

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.

Eduardo Romero
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.
Kris