Re: Date validation issue
- From: "NC" <nc@xxxxxxxxx>
- Date: 27 Jun 2005 08:36:07 -0700
ndsoumah@xxxxxxxxx wrote:
>
> I'm having a hard time trying to validate some dates.
> I have a form that accept dates from users in this format
> (YYYY-mm-dd). I've been looking at the available datetime
> functions and I'm still clueless as to how they can help
> me achieve my goals.
function is_valid_date($datestring) {
if (date('Y-m-d', strtotime($datestring)) == $datestring) {
return true;
} else {
return false;
}
}
Usage examples:
is_valid_date('2005-05-22'); // returns true
is_valid_date('2005-05-32'); // returns false
The function assumes leading zeros are mandatory. If they
are not, here's a revision that will fix it:
function is_valid_date($datestring) {
list($y, $m, $d) = explode('-', $datestring);
list($yy, $mm, $dd) = explode('-', date('Y-m-d',
strtotime($datestring)));
if (($y == $yy) and ($m == $mm) and ($d == $dd)) {
return true;
} else {
return false;
}
}
Cheers,
NC
.
- References:
- Date validation issue
- From: ndsoumah@xxxxxxxxx
- Date validation issue
- Prev by Date: Re: escape charecters
- Next by Date: Call for LAMP Standardization -- Installations/User-Group Admin
- Previous by thread: Re: Date validation issue
- Next by thread: Database Row Locking WIth PHP
- Index(es):
Relevant Pages
|