Page 1 of 2

How to check date is in the format YYYY-MM-DD

Posted: Tue Oct 21, 2003 10:25 am
by JayBird
i want people to enter a date in a form and i want it to be in the format YYYY-MM-DD.

How can i make sure this is the case using PHP?

Thanks

Mark

Posted: Tue Oct 21, 2003 10:42 am
by mrvanjohnson
Just to make sure I understand your question, you want to make sure the user input into a form is in the format YYYY-MM-DD?

The best way I have found to address this is to create 3 drop downs. One with years in it, the next with month, and the third with days.
Then during the validation stage I bring all three of the fields together to create a date input to the datebase.

Posted: Tue Oct 21, 2003 10:45 am
by JayBird
yeah, i could do that, but it won't really fit into my application.

But you did understand the question correctly.

I want to return an error if the date insn't entered in the correct format.

Mark

Posted: Tue Oct 21, 2003 10:46 am
by nigma
Have you tried comparing the date they entered with the date that is returned by the date() function?

Not sure about the following, but worth a shot.

Code: Select all

if (date("Y-m-d") == $dateEntered)
{
//do something
}
else
{
//do something else
}
You could probably do a similar thing using regular expressions and the date() function.

Posted: Tue Oct 21, 2003 10:48 am
by Nay
Or maybe Regular Expressions?

Code: Select all

function check($word) {
return ereg("^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$", $word);
}
I'm not sure about the syntax of that :P, but that's how I'd do it - with some futhur research.

-Nay

Posted: Tue Oct 21, 2003 10:48 am
by JayBird
The code you tried wouldn't work unless the date that was being entered in the form was todays date, but 99.9% of the time, the date being entered will be a date in the past.

Mark

Posted: Tue Oct 21, 2003 10:50 am
by markl999
You can check the 'format' with something like:

Code: Select all

if(preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $datein)){
    //it's ok
}else{
    //it's bad
}
The problem is checking it's 'valid' , i.e September can't have 31 days, so 2003-09-31 is invalid. Might want to look at something like http://pear.php.net/package/Validate

Posted: Tue Oct 21, 2003 10:51 am
by Nay
YAY! Thanks mark, now I don't have to do the way I normally do :P.

-Nay

Posted: Tue Oct 21, 2003 11:12 am
by jason
markl999: Put that snippet into the new Code Snippet section:

viewforum.php?f=29

Would go good there.

Posted: Tue Oct 21, 2003 11:17 am
by markl999
ok, done ;) Didn't recognise you there for a second without your newbienetwork tag. Say hello to Piera for me :o

Posted: Tue Oct 21, 2003 11:27 am
by jason
Oh, wait, are you the MarkL from #php?


Edit Note: viewtopic.php?p=65495

Posted: Tue Oct 21, 2003 11:34 am
by markl999
Yup. Long time no speak :o

Posted: Tue Oct 21, 2003 11:36 am
by jason
Oh cool, I didn't know you had an account here. Schweet!

Yeah, long time no speak indeed. I should get back on #php more often...just so busy.

Anyways, will let Piera know you said hi.

Posted: Tue Oct 21, 2003 12:14 pm
by evilMind
Checking for validity:

Code: Select all

function MyCheckDate( $postedDate ) {
   if ( ereg("^[0-9]{4}-[01][0-9]-[0-3][0-9]$",$postedDate) ) {
      list( $year , $month , $day ) = explode('-',$postedDate);
      return( checkdate( $month , $day , $year ) );
   } else {
      return( false );
   }
}
ereg breakdown:
start of the line...
exactly four times any of the characters 0-9
a -
exactly one time one of the characters 1 OR 0
exactly one time one of the characters 0-9
a -
exactly one time one of the characters 0-3
exactly one time one of the characters 0-9
until the end of the line

That way you verify before calling checkdate that the person didn't enter 2003-99-99 or something silly like that. You could break it down to check for the current year also by changing the [0-9]{4}- to 2003-

Posted: Wed Oct 22, 2003 2:33 am
by devork
yes regular expressions are very helpful in pattern matching...
and then validating them...