Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将数据从excel上传到mysql?(使用php)_Php_Mysql_Date_Cakephp - Fatal编程技术网

如何将数据从excel上传到mysql?(使用php)

如何将数据从excel上传到mysql?(使用php),php,mysql,date,cakephp,Php,Mysql,Date,Cakephp,我正在尝试将一些数据从excel上传到mysql,我正在使用php。当我上传日期为2015年5月14日时,无法正确上传。它的格式是1970-01-01。我很累,在谷歌搜索 这是我的密码 if ($ext == 'xlsx' || $ext == 'xls') { try { $inputFileType = PHPExcel_IOFactory::identify($targe

我正在尝试将一些数据从excel上传到mysql,我正在使用php。当我上传日期为2015年5月14日时,无法正确上传。它的格式是1970-01-01。我很累,在谷歌搜索

这是我的密码

if ($ext == 'xlsx' || $ext == 'xls') {
                            try {
                                $inputFileType = PHPExcel_IOFactory::identify($target_path);
                                $objReader = PHPExcel_IOFactory::createReader($inputFileType,'CSV');
                                $objReader->setReadDataOnly(true);
                                $objPHPExcel = $objReader->load($target_path);
                            } catch (Exception $e) {
                                die('Error loading file "' . pathinfo($target_path, PATHINFO_BASENAME) . '": ' . $e->getMessage());
                            }
                            $sheet = $objPHPExcel->getSheet(0);
                            $highestRow = $sheet->getHighestRow();
                            $highestColumn = $sheet->getHighestColumn();
                        }else{
                             //In case of CSV
                            if (($handle = fopen($target_path, "r")) !== FALSE) {
                                while (($data = fgetcsv($handle)) !== FALSE) {
                                    $csvData[] = $data;
                                }
                                fclose($handle);
                                $highestRow = count($csvData);

                            }
                        }

                        $rowData = array();
                        for ($row = 2; $row <= $highestRow; $row++) {
                            if ($ext == 'xlsx' || $ext == 'xls') {
                                //  Read a row of data into an array
                                $test = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
                                $rowData[] = $test[0];

                            }
                        }



                        if ($ext == 'csv') {
                            $rowData = $csvData;
                        }
                        $variable = array();
                        $i = 0;
                        $targetArr = array();
                        $error = '';
                        foreach($rowData as $item){
                            if($item[0] !='' && $item[1] !='' && $item[2] !=''):
                                $variable[$i]['Customer'] = array();
                                $variable[$i]['Customer']['msisdn'] = $item[0];
                                $variable[$i]['Customer']['csactivated'] = date('y-m-d',strtotime($item[1]));
                                $variable[$i]['Customer']['csdeactivated'] = date('y-m-d',strtotime($item[2]));
                                //$targetArr[] = $variable;
                                $i++;
                            else:
                                $count =+1;
                                $error .= 'Skip this Row'.$i++.'<br>';
                                continue;
                            endif;
                        }
if($ext=='xlsx'| |$ext=='xls'){
试一试{
$inputFileType=PHPExcel\u IOFactory::identify($target\u path);
$objReader=PHPExcel_IOFactory::createReader($inputFileType,'CSV');
$objReader->setReadDataOnly(true);
$objPHPExcel=$objReader->load($target\u path);
}捕获(例外$e){
die('Error loading file'.pathinfo($target_path,pathinfo_BASENAME)。'”:'。$e->getMessage());
}
$sheet=$objPHPExcel->getSheet(0);
$highestRow=$sheet->getHighestRow();
$highestColumn=$sheet->getHighestColumn();
}否则{
//如果是CSV
if($handle=fopen($target_path,“r”)!==FALSE){
while(($data=fgetcsv($handle))!==FALSE){
$csvData[]=$data;
}
fclose($handle);
$highestRow=计数($csvData);
}
}
$rowData=array();
对于($row=2;$row RANGETORRAY('A'.$row'.:'.$HIGHESTCLUMN.$row,NULL,TRUE,FALSE));
$rowData[]=$test[0];
}
}
如果($ext=='csv'){
$rowData=$csvData;
}
$variable=array();
$i=0;
$targetar=array();
$error='';
foreach($rowData作为$item){
如果($item[0]!=''&&$item[1]!=''&&&$item[2]!='':
$variable[$i]['Customer']=array();
$variable[$i]['Customer']['msisdn']=$item[0];
$variable[$i]['Customer']['csactivated']=date('y-m-d',strottime($item[1]);
$variable[$i]['Customer']['csdeactivated']=date('y-m-d',strottime($item[2]);
//$TARGETAR[]=$variable;
$i++;
其他:
$count=+1;
$error.=“跳过此行”。$i++.
; 继续; endif; }

当我导入excel时,我得到了类似于42104.592743056的日期,尽管它不是我的日期格式………而mysql插入像1970-01-01

,因为您告诉PHPExcel在加载电子表格时忽略电子表格中的任何格式(
$objReader->setReadDataOnly(true);
),例如日期/时间格式,您只能获取MS Excel的内部时间戳值。您需要调用PHPExcel的内置函数手动转换日期值:

要么:

PHPExcel_Shared_Date::ExcelToPHPObject(42104.592743056);
将MS Excel日期/时间值转换为PHP日期时间对象

将MS Excel值转换为Unix时间戳的步骤

然后,您可以使用适当的PHP函数以您喜欢的方式对其进行格式化



或者,加载电子表格时不要抑制格式信息,并告诉
rangeToArray()
若要应用格式设置

您的代码在哪里?添加一些代码。您尝试了什么?如果可能,请观看问题的演示。我不明白我在哪里使用此代码PHPExcel\u Shared\u Date::ExcelToPHPObject(42104.592743056)@Mark Baker您可以在
我得到日期(如42104.592743056)的地方使用它来将
42104.592743056
MS Excel日期值转换为Unix时间戳或PHP日期时间对象,而不是使用
strotime($item[1])
PHPExcel_Shared_Date::ExcelToPHP(42104.592743056);