Page 1 of 1

validate m/d/Y date - How to ?

Posted: Sun Aug 31, 2008 1:42 pm
by soulmasta
Hi everybody,

can you please show me how we can validate with PHP an input box that accepts date in such form: m/d/ Y or (DD/MM/YYYY) ? :roll: :?: :crazy:

i will apreciate it !

Re: validate m/d/Y date - How to ?

Posted: Sun Aug 31, 2008 1:48 pm
by Oren
checkdate() and you might need explode() as well depending on you input box format (1 box or 3?)

Re: validate m/d/Y date - How to ?

Posted: Sun Aug 31, 2008 2:03 pm
by soulmasta
one input box sir the user should be able to write down the date in the form of Month/Day/Year <=> MM/DD/YYYY :)

something like that seems to be correct :

Code: Select all

<?php
/**
* check a date in the Italian format
*/
function checkData($date)
{
    if (!isset($date) || $date=="")
    {
        return false;
    }
   
    list($dd,$mm,$yy)=explode("/",$date);
    if ($dd!="" && $mm!="" && $yy!="")
    {
        return checkdate($mm,$dd,$yy);
    }
   
    return false;
}
?>
(taken from : http://docs.php.net/manual/en/function. ... .php#71529)

right sir ?

Re: validate m/d/Y date - How to ?

Posted: Sun Aug 31, 2008 2:10 pm
by Oren
Wrong. See this line: list($dd,$mm,$yy)=explode("/",$date);
It means the code will validate a date in the format: DD/MM/YYYY, you wanted: MM/DD/YYYY
Change this line to: list($mm,$dd,$yy)=explode("/",$date); unless you want to validate a date in the DD/MM/YYYY format.

Re: validate m/d/Y date - How to ?

Posted: Sun Aug 31, 2008 2:18 pm
by soulmasta
oh yeh am sorry, i wanted MM/DD/YYYY :banghead:

by changing only this part right here its allright

Code: Select all

list($mm,$dd,$yy)=explode("/",$date);
many thanks sir ;)

Is there any other cases of worries, any other points that i have to take care about when am validating date ?

Re: validate m/d/Y date - How to ?

Posted: Sun Aug 31, 2008 2:32 pm
by soulmasta
also i forgot,
which mysql datatype for this date's input field i must declare for best result (is any "numeral" datatype in mysql) ?
my target is this input box to include only numbers and the right-slash (/) ..how can i achive this ?

Re: validate m/d/Y date - How to ?

Posted: Sun Aug 31, 2008 2:56 pm
by greyhoundcode
You can create a datetimestamp in PHP and use the equivalent datetimestamp type on your MySQL database table.

Re: validate m/d/Y date - How to ?

Posted: Sun Aug 31, 2008 3:39 pm
by Oren
Or you can just use the DATE type :wink:
And yes, there are many worries... how can you be sure that your users will type the date like MM/DD/YYYY ? how can you be sure they will be using / (slash) between the day, month and year?
Of course there are all the other security worries but this is beyond the scope of this topic...