Re: Date validation

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

 



I'm posting here for completeness as I've now rolled my own date
validator (code follows my sig). It allows almost all valid 'English'
formats except ordinal day values (1st, 3rd, etc.) Because I'm in UK
and writing for a UK audience, I've parsed ambiguous dates as d/m/y.
Those writing for places where m/d/y is the convention will probably
want to alter the tests in the second if ... elseif construct. It's a
bit long-winded, but by all means use or modify if it's any use to
you.

Best regards,

Geoff

-------------------8<----------------------------------------------

function is_date ($str){
    $dateOK = TRUE;
    if (stristr($str, "/")){
        $aryDate = explode ("/", $str);
    } elseif (stristr($str, "-")){
        $aryDate = explode ("-", $str);
    } elseif (stristr($str, " ")){
        $aryDate = explode (" ", $str);
    } else {
        $dateOK = FALSE;
    }
    if (count($aryDate) != 3){
        // we don't have the correct number of date parts
        $dateOK = FALSE;
    }
    if ($dateOK) {
        // trim any leading or trailing whitespace
        for ($i = 0; $i < count($aryDate); $i++){
            $aryDate[$i] = trim($aryDate[$i]);
        }
        // determine which value is month and which is day
        if (!is_numeric($aryDate[0])){
            $month = $aryDate[0];
            $day = $aryDate[1];
        } elseif (!is_numeric($aryDate[1])){
            $day = $aryDate[0];
            $month = $aryDate[1];
        } elseif ($aryDate[1] <= 12){
            $day = $aryDate[0];
            $month = $aryDate[1];
        } else {
            $month = $aryDate[0];
            $day = $aryDate[1];
        }
        $year = $aryDate[2];
        // Expand 2-digit years to 4 digits. Cut-off is current year + 10.
        if (strlen($year) != 4){
            $now = date('y') + 10;
            $year = $year - $now < 0 ? '20' . $year : '19' . $year;
            // check for correct year length
            if (strlen($year) != 4){
                // we didn't start with two digits
                $dateOK = FALSE;
            }
        }
        // Convert month names to month numbers
        if (!is_numeric($month)){
            $aryMonth = array('nowt', 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');
            $month = strtolower(substr($month, 0, 3));
            foreach ($aryMonth AS $key => $value){
                if ($value == $month){
                    $month = $key;
                }
            }
        }
        $dateOK = $dateOK && checkdate($month, $day, $year);
    }
    return ($dateOK);
}


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