How to check date is in the format YYYY-MM-DD
Moderator: General Moderators
How to check date is in the format YYYY-MM-DD
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
How can i make sure this is the case using PHP?
Thanks
Mark
- mrvanjohnson
- Forum Contributor
- Posts: 137
- Joined: Wed May 28, 2003 11:38 am
- Location: San Diego, CA
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.
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.
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.
You could probably do a similar thing using regular expressions and 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
}Or maybe Regular Expressions?
I'm not sure about the syntax of that
, but that's how I'd do it - with some futhur research.
-Nay
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);
}-Nay
You can check the 'format' with something like:
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
Code: Select all
if(preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $datein)){
//it's ok
}else{
//it's bad
}
Last edited by markl999 on Tue Oct 21, 2003 10:51 am, edited 1 time in total.
markl999: Put that snippet into the new Code Snippet section:
viewforum.php?f=29
Would go good there.
viewforum.php?f=29
Would go good there.
Last edited by jason on Tue Oct 21, 2003 11:34 am, edited 2 times in total.
Checking for validity:
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-
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 );
}
}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-