phpexcel是php中专业来操作excel表格的一个php插件了,下文我们就来看看phpexcel读取excel表格时间的例子,希望下文能够帮助到各位。
编辑通过excel表格修改了大批的产品价格和促销时间,让我们技术批量导入到线上数据库。
这样对于我们来说是一件在简单不过的事情了,保护phpexcel导表利器,瞬间解决问题。
可是,进入数据库一看:蒙了,导入的时间格式有问题,展示的不是时间,是数字,郁闷中。
然后通过php输出,果然不是时间的格式。
百度一遍发现,phpexcel里面提供了这样的方法getFormattedValue()来读出时间的,将getValue()换成
getFormattedValue();
$abc = $currentSheet->getCell ( 'A' . $currentRow )->getFormattedValue ();
这样就可以顺利的读出excel表里的时间,重新更新数据库,OK。
下面看个例子
<?php 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; }
本文链接:
收藏随意^^请保留教程地址.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
