Re: validating mysql bound date

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Something like this, perhaps:

preg_match('/^((19|20)[0-9]{2})-([0-1]?[0-9])-([0-3]?[0-9])$/',
$input, $date_parts);
var_dump($date_parts);

This doesn't completely rule out bogus dates such as 2008-02-30 however.

I think MySQL would just convert that to MAR 1, 2008 anyway...

But if you want that degree of comparison:

list(,$year, $month, $day) = $date_parts;
$unix = mktime(1, 0, 0, $month, $day, $year);
list($y, $m, $d) = date('Y-m-d', $unix);
if ($year != $y || $month != $m || $day != $d){
  //handle invalid date input here
}

If you do all of that (which is really 5 lines of code) you should be
pretty sure it's a correct/valid date.

On Tue, March 4, 2008 10:12 pm, Larry Brown wrote:
> Thanks,
>
> I ended up doing:
>
> $incomingQuestDatePieces = explode("-", $incomingQuestDate );
>
> if(checkdate($incomingQuestDatePieces[1],$incomingQuestDatePieces[2],
> $incomingQuestDatePieces[0]))
> {
> 	return true;
> }
> else
> {
> 	return false;
> }
>
>
> I was just wondering since a lot of people have to verify correct
> format
> of the date when working with mysql that there might be some built in
> that is faster etc.
>
> Thanks though...
>
> On Wed, 2008-03-05 at 14:34 +1100, Chris wrote:
>> Larry Brown wrote:
>> > Its been a long week already... YYYY-MM-DD.
>> >
>> > On Tue, 2008-03-04 at 20:16 -0500, Larry Brown wrote:
>> >> Does anyone know if there is a builtin function for checking the
>> >> formatting of an incoming date to verify it is YYYY/MM/DD.  I
>> know how
>> >> to convert between formats but want a quick way to check an
>> incoming
>> >> variable to ensure it will be handled properly by mysqld.
>>
>> I normally provide dropdown fields for each (except the year which
>> is a
>> text-field) and put it together how I need it. Validate each part
>> separately and you're off and racing.
>>
>> If you accept any date you'll probably have to split it up first but
>> the
>> principles will be the same.
>>
>>
>> $date = '0000-00-00';
>> // if they didn't use exactly two dashes? invalid
>> if (substr_count($date, '-') !== 2) {
>>    die("Invalid date");
>> }
>>
>> list($year, $month, $day) = explode('-', $date);
>> if (strlen($year) != 4) {
>>    die("Invalid year");
>> }
>>
>>
>> and so on.
>>
> --
> Larry Brown <larry.brown@xxxxxxxxxxxxxxxxxxxxx>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux