Re: checking user input of MM-DD-YYYY

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

 



On Jan 15, 2008 11:51 AM, Adam Williams <awilliam@xxxxxxxxxxxxxxxx> wrote:
>
>
> Andrew Ballard wrote:
> > Just curious why you won't take 1-15-2008. Once you validate it, you
> > can always assign it to a variable as either a timestamp or a DateTime
> > object and then format it however you want when you display it, send
> > it to a database, or whatever you are doing with the date.
> >
> > FWIW, what you have above will also accept 42-75-2008.
> >
> > Andrew
> >
> >
> Because I'm inserting it into MySQL as a date conversion from American
> date to a MySQL date field. %m must be ##, %d must be ##, and %Y must be
> ####. so if %m or %d is set to 1 - 9 and not 01 - 09 it will error.
>
>
> $mysqli_insert_sql = "INSERT INTO contract (user_id, cwcv,
> amount, responsibility, length_start, length_end, stage, title, lastmod,
> divdirdate)
> VALUES ( '$user_id', '". $_POST["cwcv"]."', '".$_POST["amount"]."',
> '".$_POST["responsibility"]."',
> STR_TO_DATE('".$_POST["length_start"]."', '%m-%d-%Y'),
> STR_TO_DATE('".$_POST["length_end"]."', '%m-%d-%Y'), '1',
> '".$_POST["title"]."', now(), now())";

    Then don't bother with date validation in that respect.  Instead,
just use tandem functions:

<?
function mysql_date($m,$d,$y) {
        if(!is_numeric($m) || !is_numeric($d) || !is_numeric($y)) {
                return "Failed due to improper input";
        }
        return date("m-d-Y",mktime(0,0,0,$m,$d,$y));
}

echo mysql_date($month,$day,$year)."\n";
?>

-- 
</Dan>

Daniel P. Brown
Senior Unix Geek and #1 Rated "Year's Coolest Guy" By Self Since
Nineteen-Seventy-[mumble].

-- 
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