phpcsvfgetcsv

how to have checking on date on csv upload PHP


$ayear=date("Y") + 1 ; 
$lyear=date("Y") - 1 ;
$cyear=date("Y") ;  

some code here

        fgetcsv($handle);
        while($data=fgetcsv($handle)){

            $item0=($data[0]);
            $item1=($data[1]);
            if(!empty($data[2])){
                if((date('Y', strtotime(($data[2]))) >= $lyear) && (date('Y', strtotime(($data[2]))) <= $ayear) ){
                    $item2=date('Ymd', strtotime(($data[2])));
                }else{
                    header("Location: index.php?success=date");
                }

            }else{
                // $item2=($data[2]);
                header("Location: index.php?success=date");
                break;
            }
            $item3=($data[3]);

how can i check the years on the column date on upload process it needs to allow years that is current year and next year and last year

example current year is 2022 it needs to allow 2023 , 2022 and 2021

i aim to prevent upload dates that are way beyond, like "5029" or "1990"


Solution

  • $ayear=date("Y") + 1 ;
    $lyear=date("Y") - 1 ;
    $cyear=date("Y") ;
    $arryear = array($ayear,$cyear,$lyear);
    

    some code here

    if(in_array(date('Y', strtotime(($data[2]))), $arryear)){
        $item2=date('Ymd', strtotime(($data[2])));
    }else{
        header("Location: index.php?success=date");
        break;
    }