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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

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

Post 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
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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
Last edited by markl999 on Tue Oct 21, 2003 10:51 am, edited 1 time in total.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

YAY! Thanks mark, now I don't have to do the way I normally do :P.

-Nay
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

markl999: Put that snippet into the new Code Snippet section:

viewforum.php?f=29

Would go good there.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

ok, done ;) Didn't recognise you there for a second without your newbienetwork tag. Say hello to Piera for me :o
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Oh, wait, are you the MarkL from #php?


Edit Note: viewtopic.php?p=65495
Last edited by jason on Tue Oct 21, 2003 11:34 am, edited 2 times in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Yup. Long time no speak :o
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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.
evilMind
Forum Contributor
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

Post 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-
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

yes regular expressions are very helpful in pattern matching...
and then validating them...
Post Reply