how to have checking on date on csv upload PHP

37 views Asked by At
$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"

1

There are 1 answers

1
user6001531 On BEST ANSWER
$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;
}