A function - Check a date string whether valid.

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
ctmaster
Forum Newbie
Posts: 7
Joined: Wed Jul 08, 2009 10:45 pm

A function - Check a date string whether valid.

Post by ctmaster »

follow the topic, i wanna get a function for validating a date string whether valid or not.

for example "2009-02-02" is a valid date, but "2009-02-30" is a invalid date.
iamngk
Forum Commoner
Posts: 50
Joined: Mon Jun 29, 2009 2:20 am

Re: A function - Check a date string whether valid.

Post by iamngk »

yes it is possible with checkdate function in php.

please see the following code:


$dt="02/28/2007";
$arr=split("/",$dt); // splitting the array
$mm=$arr[0]; // first element of the array is month
$dd=$arr[1]; // second element is date
$yy=$arr[2]; // third element is year
If(!checkdate($mm,$dd,$yy)){
echo "invalid date";
}else {
echo "Entry date is correct";
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: A function - Check a date string whether valid.

Post by pickle »

You might also be able to just throw that at strtotime()
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: A function - Check a date string whether valid.

Post by Darhazer »

iamngk wrote:yes it is possible with checkdate function in php.

please see the following code:
...
}
You can use list() to cut off few lines...

Code: Select all

 
$dt="02/28/2007";
list($mm, $dd, $yy) = split("/",$dt); // splitting the array
 
iamngk
Forum Commoner
Posts: 50
Joined: Mon Jun 29, 2009 2:20 am

Re: A function - Check a date string whether valid.

Post by iamngk »

thanks Darhazer for your information. :)
Post Reply