代码片断收集

1 生成excel

<?php
include "PHPExcel.php";
$lines=file("result351.txt");
$objPHPExcel = new PHPExcel();
$num=1;
$objPHPExcel->getActiveSheet()->setCellValue('A'.$num, "线上开始时间");
$objPHPExcel->getActiveSheet()->setCellValue('B'.$num, "线上结束时间");
$objPHPExcel->getActiveSheet()->setCellValue('C'.$num, "excel开始时间");
$objPHPExcel->getActiveSheet()->setCellValue('D'.$num, "excel结束时间");
$objPHPExcel->getActiveSheet()->setCellValue('E'.$num, "合同号");
$num=2;

foreach($lines as $v){
    $line_array=explode('|',$v);
    $online_startime=$line_array[0];
    $online_stoptime=$line_array[1];
    $excel_startime=$line_array[2];
    $excel_stoptime=$line_array[3];
    $online_startime_array=explode(':',$online_startime);
    $a_online_startime=$online_startime_array[1];
    $online_stoptime_array=explode(':',$online_stoptime);
    $a_online_stoptime=$online_stoptime_array[1];
    $excel_startime_array=explode(":",$excel_startime);
    $a_excel_startime=$excel_startime_array[1];
    $excel_stoptime_array=explode(":",$excel_stoptime);
    $a_excel_stoptime=$excel_stoptime_array[1];

    list($xx,$htnum)=explode(":",$line_array[4]);

    $objPHPExcel->getActiveSheet()->setCellValue('A'.$num, $a_online_startime);
    $objPHPExcel->getActiveSheet()->setCellValue('B'.$num, $a_online_stoptime);
    $objPHPExcel->getActiveSheet()->setCellValue('C'.$num, $a_excel_startime);
    $objPHPExcel->getActiveSheet()->setCellValue('D'.$num, $a_excel_stoptime);
    $objPHPExcel->getActiveSheet()->setCellValue('E'.$num, $htnum);
    $num=$num+1;
}
$name="11111";
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$name.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

//php二维数组排序 
    usort($array, function($a, $b) {
        $astimel = strtotime($a['start_time']);
        $bstimel = strtotime($b['start_time']);
        if ($astimel == $bstimel)
            return 0;
        return ($astimel < $bstimel) ? -1 : 1;
    });



//phpexcel导入 

require_once('PHPExcel/IOFactory.php');
$filePath = '123.xls';
$fileType = PHPExcel_IOFactory::identify($filePath); //文件名自动判断文件类型
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($filePath);

$currentSheet = $objPHPExcel->getSheet(0); //第一个工作簿
$allRow = $currentSheet->getHighestRow(); //行数
$result=array();
for($currentRow = 2;$currentRow<=$allRow;$currentRow++) {
    $name = (string)$currentSheet->getCell('G'.$currentRow)->getValue();//
    $jc = (string)$currentSheet->getCell('AG'.$currentRow)->getValue();//
    $bh = (string)$currentSheet->getCell('D'.$currentRow)->getValue();//编号
    $contract_number = (string)$currentSheet->getCell('E'.$currentRow)->getValue();
    //$startime = excelTime($currentSheet->getCell('J'.$currentRow)->getValue());
    $startime=gmdate("Y-m-d H:i:s",PHPExcel_Shared_Date::ExcelToPHP($currentSheet->getCell('J'.$currentRow)->getValue()));
    //$endtime = excelTime($currentSheet->getCell('K'.$currentRow)->getValue());
    $endtime=gmdate("Y-m-d H:i:s",PHPExcel_Shared_Date::ExcelToPHP($currentSheet->getCell('K'.$currentRow)->getValue()));
    if(empty($bh)){
        continue;
    }
    mysql_query("insert into info set company_name='{$name}',jc='{$jc}',supplier_number='{$bh}',contract_number='{$contract_number}',startime='{$startime}',endtime='{$endtime}' ");
}

#php计算两个日期之间的天数,并遍历出具体的天数

<?php
$t_min = 1473621998;
$t_max = 1479961998;

//by the way 1
$d_min = date('Y-m-d',$t_min);
$d_max = date('Y-m-d',$t_max);
$d_min_unix = strtotime($d_min);
$d_max_unix = strtotime($d_max);

$t_current = $d_min_unix;
while ($t_current <= $d_max_unix){
    $tmp = date('Y-m-d',$t_current);
    echo $tmp."<br>";
    $t_current = strtotime("+1 day",$t_current);
}


//by the way 2
for($curent=$t_min;$curent<=$t_max;$curent=strtotime("+1 day",$curent)){
    $tmp = date('Y-m-d',$curent);
    echo $tmp."<br>";
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注