Home php教程 php手册 phpexcel读取excel表格时间的例子

phpexcel读取excel表格时间的例子

May 26, 2016 am 08:19 AM
explode

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(&#39;Asia/shanghai&#39;);
/** PHPExcel_IOFactory */
require_once &#39;../Classes/PHPExcel/IOFactory.php&#39;;
$inputFileName = &#39;6081076641077444758.xls&#39;;
$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 = &#39;A&#39;; $k <= $highestColumn; $k++) {
        if ($k == &#39;M&#39; || $k == &#39;O&#39;) //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(&#39;GregorianToJD&#39;)) {
        if (is_numeric($date)) {
            $jd = GregorianToJD(1, 1, 1970);
            $gregorian = JDToGregorian($jd + intval($date) - 25569);
            $date = explode(&#39;/&#39;, $gregorian);
            $date_str = str_pad($date[2], 4, &#39;0&#39;, STR_PAD_LEFT) . "-" . str_pad($date[0], 2, &#39;0&#39;, STR_PAD_LEFT) . "-" . str_pad($date[1], 2, &#39;0&#39;, STR_PAD_LEFT) . ($time ? " 00:00:00" : &#39;&#39;);
            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" : &#39;&#39;);
    }
    return $date;
}
Copy after login


本文链接:

收藏随意^^请保留教程地址.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP explode function and solve errors How to use PHP explode function and solve errors Mar 10, 2024 am 09:18 AM

The explode function in PHP is a function used to split a string into an array. It is very common and flexible. In the process of using the explode function, we often encounter some errors and problems. This article will introduce the basic usage of the explode function and provide some methods to solve the error reports. 1. Basic usage of the explode function In PHP, the basic syntax of the explode function is as follows: explode(string$separator,string$stri

Common errors and solutions when using the explode function in PHP Common errors and solutions when using the explode function in PHP Mar 11, 2024 am 08:33 AM

Title: Common errors and solutions when using the explode function in PHP In PHP, the explode function is a common function used to split a string into an array. However, some common errors can occur due to improper use or incorrect data format. This article will analyze the problems you may encounter when using the explode function, and provide solutions and specific code examples. Mistake 1: The delimiter parameter is not passed in. When using the explode function, one of the most common mistakes is that the delimiter parameter is not passed in.

Split and merge strings using the explode and implode functions Split and merge strings using the explode and implode functions Jun 15, 2023 pm 08:42 PM

In PHP programming, processing strings is a frequently required operation. Among them, splitting and merging strings are two common requirements. In order to perform these operations more conveniently, PHP provides two very practical functions, namely the explode and implode functions. This article will introduce the usage of these two functions, as well as some practical skills. 1. The explode function The explode function is used to split a string according to the specified delimiter and return an array. Its function prototype is as follows: arra

Split string into array using PHP function 'explode' Split string into array using PHP function 'explode' Jul 24, 2023 pm 11:09 PM

Use the PHP function "explode" to split a string into an array. In PHP development, you often encounter situations where you need to split a string according to the specified delimiter. At this time, we can use PHP's built-in function "explode" to convert string to array. This article will introduce how to use the "explode" function to split a string and give relevant code examples. The basic syntax of the "explode" function is as follows: arrayexplode(

What should I do if php explode reports an error? What should I do if php explode reports an error? Jan 18, 2023 am 10:13 AM

Solution to php explode error: 1. Find and open the erroneous PHP code; 2. Find the explode function part; 3. Modify the code to "dump(explode(',',$str));die;", that is, use Just comma-separate the array.

Use PHP function 'explode' to split a string into multiple substrings Use PHP function 'explode' to split a string into multiple substrings Jul 25, 2023 pm 06:29 PM

Use the PHP function "explode" to split a string into multiple substrings. In PHP programming, we often encounter situations where we need to split a string into multiple substrings. At this time, PHP provides a very convenient function "explode" that can help us easily implement this function. The syntax of the "explode" function is as follows: arrayexplode(string$delimiter,string$string[,in

How to use explode function to split string in PHP How to use explode function to split string in PHP Jun 26, 2023 pm 12:03 PM

In the PHP language, there are many basic functions that can help us process strings quickly and efficiently. Among them, the explode function is a very practical string splitting function. It can split a string into arrays according to the specified delimiter, and then perform more flexible string operations. In this article, we will introduce how to use the explode function to split strings in PHP. 1. The format of the explode function. The format of the explode function in the PHP language is as follows: explode(separa

What is the explode() function in PHP? What is the explode() function in PHP? Sep 04, 2023 pm 01:01 PM

In this article, learn how to use the PHPExplode() function, which is a predefined built-in PHP function. The explode function is used to "Split a string into smaller contents. The explode function in PHP enables us to break a string into smaller contents by breaking. This delimiter is called a delimiter. Syntax explode(delimiter, string, Number of elements) Parameter explosion function accepts three parameters, two of which are mandatory and one is optional. Let us discuss these three parameters. Separator This character specifies the critical point or point at which the string will be split, i.e. Whenever this character is found in a string, it symbolizes the end of one element and the beginning of another element in the array. String input string

See all articles