==edited as per request==
put it before sql query:
if(empty($finalDate))
$where = "(data >= ".$startDate.")";
else if(empty($startDate))
$where = "(data <= ".$finaltDate.")";
else
$where = "(data BETWEEN ".$startDate." AND ".$finalDate.")";
and change
(data BETWEEN ".$startDate." AND ".$finalDate.")
with
".$where."