Jump to content

Best way to check a Date?


doubledee

Recommended Posts

Currently I have an Input field for a "Written On Date".

 

What is the best way to make sure a valid date is entered?

 

Should I use Regular Expressions?

 

Some PHP Date-Checking Function?

 

Or switch to Drop-Down Lists to better control the selectable values?  (Which won't catch things like June 31?!)

 

 

Debbie

 

 

Link to comment
Share on other sites

you could have three select elements with year, month, day and then check is_numeric() on each of the fields to ensure validity

<select name='year'>
<?php for ($i=1990;$i<2020;$i++) {
echo "<option value='$i'>$i</option>";
} ?>
</select>

//do again for month, day... though month you would have the value as the numeric month and the displayed value as the month name

Link to comment
Share on other sites

The Anonymous post on that page is correct. Checkdate, the way he tried to use it, should not be used as validation to check if a date is correct (the format or if it contains any XSS scripting.) That's not its' job. The point of checkdate is to validate if the month, day, and year numerical values it is passed is a valid date (i.e. One of the things you asked - What is the best way to make sure a valid date is entered?) The preg_split pattern that he used isn't specific to the expected data, so of course it is possible to bypass it.

 

Regardless if using one field for the date (a datepicker) or three different fields (three dropdown select lists, one each for the month, day, and year), in real life, you would use a pattern that matched the format of the data you are expecting.

Link to comment
Share on other sites

The Anonymous post on that page is correct. Checkdate, the way he tried to use it, should not be used as validation to check if a date is correct (the format or if it contains any XSS scripting.) That's not its' job. The point of checkdate is to validate if the month, day, and year numerical values it is passed is a valid date (i.e. One of the things you asked - What is the best way to make sure a valid date is entered?) The preg_split pattern that he used isn't specific to the expected data, so of course it is possible to bypass it.

 

Regardless if using one field for the date (a datepicker) or three different fields (three dropdown select lists, one each for the month, day, and year), in real life, you would use a pattern that matched the format of the data you are expecting.

 

So I take my Month, Day, and Year, run them through checkdate() and if I get a "True" then I'm good to go?!

 

 

Debbie

 

 

Link to comment
Share on other sites

try something like this

 

function is_date( $str )
{
  $stamp = strtotime( $str );

  if (!is_numeric($stamp))
  {
     return FALSE;
  }
  $month = date( 'm', $stamp );
  $day   = date( 'd', $stamp );
  $year  = date( 'Y', $stamp );

  if (checkdate($month, $day, $year))
  {
     return TRUE;
  }

  return FALSE;
} 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.