PHPExcel读取excel表格时间单元格(转载)

error_reporting(E_ALL);

date_default_timezone_set('Asia/shanghai');

/** PHPExcel_IOFactory */

require_once '../Classes/PHPExcel/IOFactory.php';

$inputFileName = '6081076641077444758.xls';

$objReader = new PHPExcel_Reader_Excel5();

$objPHPExcel = $objReader->load($inputFileName);

$sheet = $objPHPExcel->getSheet(0);

$highestRow = $sheet->getHighestRow(); // 取得总行数

$highestColumn = $sheet->getHighestColumn(); // 取得总列数

$tempArray = array();

for($j=2;$j<=$highestRow;$j++){

   for($k='A';$k<=$highestColumn;$k++){ 

    if($k=='M'||$k=='O') //M列和O列是时间

        $tempArray[] = excelTime($objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue());

    else

        $tempArray[] = $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue();

    }

    print_r($tempArray);

    unset($tempArray);

}

 

function excelTime($date, $time = false) {

    if(function_exists('GregorianToJD')){

        if (is_numeric( $date )) {

        $jd = GregorianToJD( 1, 1, 1970 );

        $gregorian = JDToGregorian( $jd + intval ( $date ) - 25569 );

        $date = explode( '/', $gregorian );

        $date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT )

        ."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT )

        ."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT )

        . ($time ? " 00:00:00" : '');

        return $date_str;

        }

    }else{

        $date=$date>25568?$date+1:25569;

        /*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/

        $ofs=(70 * 365 + 17+2) * 86400;

        $date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : '');

    }

  return $date;

}

  转自:https://www.cnblogs.com/wuheng1991/p/6807059.html

posted @ 2018-11-19 13:22  thebestrule  阅读(791)  评论(0编辑  收藏  举报