Re: Date validation issue



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

.



Relevant Pages

  • Parse datetime
    ... Is there a way using DateTime.Parse to validate a date in the following format? ... Prev by Date: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to Validate a Date Filed.
    ... I'm writing, and the fourth test is just getting fancy; ... IsValid As Boolean = True) As DateTime ... > You can validate a date using Regular Expressions. ... > objects and setting culture to a country that uses the date format you ...
    (microsoft.public.dotnet.framework.aspnet)
  • Fw: Re: User input masks - Access Style
    ... > format. ... ?t is easy enough to validate whether ... The time is a duration over ... not the intended recipient, unauthorized use, forwarding, printing, storing, disclosure or copying ...
    (comp.lang.python)
  • Re: User input masks - Access Style
    ... format. ...  It is easy enough to validate whether ... The time is a duration over ... Ultimately I will need to store the ...
    (comp.lang.python)
  • Re: Treasury Bond Pricing conversion
    ... I meant that A1 and B1 are the decimal equivalents. ... time I am having hard time how to format back in xxx-xx. ... I was given a hint that I need to convert the above price format first ... e.g 110-16 is in cell A1 then formula for converting to decimal would ...
    (microsoft.public.excel.misc)