fixed reading empty rows from excel and fixed date formats

This commit is contained in:
2024-09-22 18:34:32 +04:00
parent 5f915336fc
commit a9129f4265
@@ -40,9 +40,9 @@ if(isset($_FILES['f_csv'])) {
$arr_associate = $arr_associate_data->Results;
if(count($arr_associate) > 0) { //check define columns
$spreadsheet = IOFactory::load($file, IReader::READ_DATA_ONLY);
$spreadsheet = IOFactory::load($file);
$worksheet = $spreadsheet->getActiveSheet();
$arr_info = $worksheet->toArray();
$arr_info = $worksheet->toArray();
if(count($arr_info) > 1) { //check excel rows
$arr_excel_columns = $arr_info[0];
$arr_need_columns = array();
@@ -50,6 +50,29 @@ if(isset($_FILES['f_csv'])) {
array_push($arr_need_columns, "Report no.");
array_push($arr_need_columns, "Part No.");
// remove empty rows
$arr_info = array_filter($arr_info, function($row) {
return !empty(array_filter($row));
});
// find date date in $arr_info formatted as m/d/yyyy and convert to yyyy-mm-dd
$rowCount = count($arr_info);
for ($i = 0; $i < $rowCount; $i++) {
$colCount = count($arr_info[$i]);
for ($j = 0; $j < $colCount; $j++) {
$value = $arr_info[$i][$j];
// Check if the value is a string that matches the m/d/yyyy format
if (is_string($value) && preg_match('/^\d{1,2}\/\d{1,2}\/\d{4}$/', $value)) {
// Convert to DateTime and format to Y-m-d
$dateTime = DateTime::createFromFormat('m/d/Y', $value);
if ($dateTime) {
$arr_info[$i][$j] = $dateTime->format('Y-m-d'); // Store the formatted date
}
}
}
}
foreach($arr_associate as $item) {
array_push($arr_need_columns, $item['headerfile']);
}