validate m/d/Y date - How to ?

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

Post Reply
soulmasta
Forum Newbie
Posts: 17
Joined: Fri Aug 15, 2008 4:50 pm

validate m/d/Y date - How to ?

Post 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 !
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

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

Post by Oren »

checkdate() and you might need explode() as well depending on you input box format (1 box or 3?)
soulmasta
Forum Newbie
Posts: 17
Joined: Fri Aug 15, 2008 4:50 pm

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

Post 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 ?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

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

Post 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.
soulmasta
Forum Newbie
Posts: 17
Joined: Fri Aug 15, 2008 4:50 pm

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

Post 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 ?
soulmasta
Forum Newbie
Posts: 17
Joined: Fri Aug 15, 2008 4:50 pm

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

Post 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 ?
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

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

Post by greyhoundcode »

You can create a datetimestamp in PHP and use the equivalent datetimestamp type on your MySQL database table.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

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

Post 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...
Post Reply