PHPBuilder - Date Validator



RSS Twitter
Snippets Calendars Dates

Date Validator

by: Riccardo Pasquini
|
February 6, 2001

Version: 1.0

Type: Function

Category: Calendars/Dates

License: GNU General Public License

Description: a date validator :) warning Y10K bug



//Riccardo Pasquini
//07/02/2001
//for infos r.pasquini@www.doublet.it

function chkDate($txtData)
{
    //controlla della data     //date validation
    //controllo del formato gg/mm/aaaa  e recupero dei componenti della data :)       //Date Format DD/MM/YYYY
    if(ereg("([0-9]{2})/([0-9]{2})/([0-9]{4})", $txtData, $aDate))    //$aDate[1]->GG, $aDate[2]->MM e $aDate[3]->AAAA
    {   //formato corretto controlla i valori
        $aGiorni=array(0,31,28,31,30,31,30,31,31,30,31,30,31);
        if($aDate[3]<2000 || $aDate[3]>9999) //y10k bug :)))
             $errMex.=((strlen($errMex)>0)?"<br>":"")."Anno non valido";    //not a valid Year
        else
             if($aDate[2]<1 || $aDate[2]>12)
                 $errMex.=((strlen($errMex)>0)?"<br>":"")."Mese non valido";     //not a valid Month
             else
             {
                 $giorni=($aGiorni[$aDate[2]-0]+((2==$aDate[2])?((!($aDate[3]%4) && $aDate[3]%100) || !($aDate[3]%400)):0));
                 if($aDate[1]<1 || $aDate[1]>$giorni)
                     $errMex.=((strlen($errMex)>0)?"<br>":"")."Giorno non valido";     //not a valid Day
                 else
                     return true;
             }
    }
    else
        $errMex.=((strlen($errMex)>0)?"<br>":"")."Formato della data errato (GG/MM/AAAA)";    //not a valid date format

    return $errMex;
}

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Riccardo Pasquini

Comment:



Comment:

(Maximum characters: 1200). You have characters left.