Phpexcel accurate reading of Excel table time

Source: Internet
Author: User
Phpexcel correct reading of Excel table time
Using Phpexcel to read the Excel file, read out the time column appears 5 digits, not want to time (do not know why), search online a exceltime function can correctly convert the time
Brand website Construction: http://www.ucantech.com/

PHP code

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 (); Total number of rows obtained
$highestColumn = $sheet->gethighestcolumn (); Total number of columns obtained
$tempArray = Array ();
for ($j =2; $j <= $highestRow; $j + +) {
for ($k = ' A '; $k <= $highestColumn; $k + +) {
if ($k = = ' M ' | | $k = = ' o ')//m column and O column is time
$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;
} (fblww-0112)
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.