我有一个我的csv文件的格式,你可以看到有一个“2013年7月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文件中这种格式的adittional它在我使用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;
}
}
答案 0 :(得分:0)
将“JAN.7,2013”转换为2013-01-07。试试这个
$str = 'JAN. 7, 2013';
echo date("Y-m-d", strtotime($str))
答案 1 :(得分: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];
}
}
}