I have a format of my csv file and as you can see there's a "JAN. 7, 2013" how can i convert it into a 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
Adittional in this format in csv file it is showing when i use print_r() but in the top example is not inserting Can you tell me why?
GLC-SX-MM,SOLAIRE,,AGM1620L2LU,No Records,No Records,,NA,NA,NA,NA,NA,No Records,No Records,NA,No Records,No Records
Here is my library code that i used.
Code:
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;
}
}
To convert "JAN. 7, 2013" it into a 2013-01-07.Try this one
$str = 'JAN. 7, 2013';
echo date("Y-m-d", strtotime($str))
Try using conditional operator to check the field that needed to be converted.
If I were you I'll just do this
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];
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.