0

我有我的 csv 文件格式,如您所见,有一个“2013 年 1 月 7 日”,我如何将其转换为 2013-01-07。

WS-C3750X-48P-L,SOLAIRE,,FDO1651X2H9,"JAN. 7, 2013","JAN. 6, 2014",,1 Year,CISCO,Covered Under Warranty,30 Days,NA,"JAN. 2, 2013","APR. 2, 2014",1 Year and 3 Months,888-1,12643158

csv 文件中这种格式的附加信息,当我使用 print_r() 时它会显示,但在上面的例子中没有插入你能告诉我为什么吗?

GLC-SX-MM,SOLAIRE,,AGM1620L2LU,No Records,No Records,,NA,NA,NA,NA,NA,No Records,No Records,NA,No Records,No Records

这是我使用的库代码。

代码:

class CSVReader {

    var $fields;            /** columns names retrieved after parsing */ 
    var $separator = ';';    /** separator used to explode each line */
    var $enclosure = '"';    /** enclosure used to decorate each field */

    var $max_row_size = 4096;    /** maximum row size to be used for decoding */

    function parse_file($p_Filepath) {

        $key_field = 'model,client_name,end_user,serial_num,trends_warranty_start,trends_warranty_end,client_contract_no,trends_warranty_period,vendor,support_coverage,sla,service_contract_num,support_coverage_start,support_coverage_end,support_coverage_period,trends_po,supplier_so';

        $file = fopen($p_Filepath, 'r');
        $this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
        $keys_values  = explode(',',$key_field);

        $content    =   array();
        $keys   =   $this->escape_string($keys_values);

        $i  =   0;
        while( ($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false ) { 

            if( $row != null ) { // skip empty lines
                $values =   explode(',',$row[0]);

                if(count($keys) == count($values)){
                    $arr        =   array();
                    $new_values =   array();
                    $new_values =   $this->escape_string($values);
                    for($j=0;$j<count($keys);$j++){
                        if($keys[$j] != ""){

                            $arr[$keys[$j]] =   $new_values[$j];                        
                        }
                    }


                    $content[$i]=   $arr;
                    $i++;
                }
            }
        }
        fclose($file);
        return $content;
    }

    function escape_string($data){
        $result =   array();
        foreach($data as $row){
            $result[]   =   str_replace('"', '',$row);
        }
        return $result;
    }   
}
4

2 回答 2

0

将“JAN. 7, 2013”​​转换为 2013-01-07。试试这个

$str = 'JAN. 7, 2013';
echo date("Y-m-d", strtotime($str))
于 2013-10-07T08:09:00.753 回答
0

尝试使用条件运算符检查需要转换的字段。

如果我是你,我会这样做

for($j=0;$j<count($keys);$j++){
    if($keys[$j] != ""){
        if ($j == 4 || $j == 5 || $j == 12 || $j == 13) {
            $new_values[$j] = date("Y-m-d", strtotime($new_values[$j]));
            $arr[$keys[$j]] = $new_values[$j];
        }                   
    }
}
于 2013-10-07T08:39:08.560 回答