loadEnv(__DIR__.'/../../.env'); $user = $_ENV['DB_USER']; $pass = $_ENV['DB_PASSWORD']; // connect to db $db = new PDO($dsn, $user, $pass); // prepared statement $sth = $db->prepare('insert into warranty_serial (id, sku, date_create, source, meta_info) values (:serial, :sku, :date_create, :source, :meta_info)'); // go through rows $source = 'motiv'; while (($row = fgetcsv($csv)) !== false) { // TODO: verify if these are still the headers if there are headers // possible lines in output file: // (1) header in csv file // SerialNumber,Sku,DispatchStatus,CreatedDate,InventoryStatus,CategoryID,CategoryName // (2) No available data // (3) CH2000012071,WCHD23BL-CPN00-LX,0,2020-08-11 04:05:27.090,0,4,CHAMPION MF // (4) Empty line // (5) empty sku // check if No available data if ($row[0] == 'No available data') { // skip the line error_log('No available data, skipping the line...'); continue; } // check if empty line if ($row == array(null)) { // skip error_log('Skipping empty line...'); continue; } // check if empty serial if (empty($row[0])) { $err_message = "Empty serial. " . "\n"; fwrite($output_fh, $err_message); continue; } // sample of line in output file: // serial number, sku, dispatch status, created date, inventory status, category id, category name // CH2000012071,WCHD23BL-CPN00-LX,0,2020-08-11 04:05:27.090,0,4,CHAMPION MF // MG2000313690,N/A,1,2021-05-14T23:47:30.6430000+08:00,0,10,GOLD $serial = trim(strtoupper($row[0])); $sku = trim(strtoupper($row[1])); $dispatch_status = trim($row[2]); $str_date_create = trim($row[3]); $inventory_status = trim($row[4]); $cat_id = trim($row[5]); $cat_name = trim(strtoupper($row[6])); error_log('Processing ' . $serial . ' and ' . $sku); // since some people cannot follow simple instructions... // check the date format on the string // try 2021-05-15T08:35:46+08:00 format on str_date_create $date_create = DateTime::createFromFormat('Y-m-d\TH:i:sP', $str_date_create); if ($date_create == false) { // try this format: 2021-05-15T08:47:20.3330000+08:00 // get the date, time and timezone from str_date_create $str_date_time = substr($str_date_create, 0, 19); $str_timezone = substr($str_date_create, 27); $str_datetime_tz = $str_date_time . $str_timezone; // create DateTime object // sample: 2021-05-15T12:16:06+08:00 $date_create = DateTime::createFromFormat('Y-m-d\TH:i:sP', $str_datetime_tz); // check if datetime object was created // if not, someone f*cked up and we have no date create if ($date_create == false) { // log the serial $message = "$serial - ERROR - " . "Invalid date format for create date." . "\n"; fwrite($output_fh, $message); continue; } } $created_date = $date_create->format('Y-m-d H:i:s'); //error_log($str_date_time); //error_log($str_timezone); //error_log($str_datetime_tz); //error_log($date_create->format('Y-m-d H:i:s')); $meta_info = [ 'dispatch_status' => $dispatch_status, 'inventory_status' => $inventory_status, 'category_id' => $cat_id, 'category_name' => $cat_name, ]; $info = json_encode($meta_info); if ($sku == 'N/A') $sku = null; $res = $sth->execute([ ':serial' => $serial, ':sku' => $sku, ':date_create' => $created_date, ':source' => $source, ':meta_info' => $info, ]); if (!$res) { // log the error $err = $sth->errorInfo(); $err_message = "$serial - ERROR - " . $err[2] . "\n"; fwrite($output_fh, $err_message); } else { // log successful adding of serial $message = "$serial - SUCCESS - " . "\n"; fwrite($output_fh, $message); } } // close file fclose($csv); fclose($output_fh);