0

使用 PHP 将 Excel 文件导入 MySQL 数据库时遇到问题。它为每个日期字段值显示一个整数值。

例如,假设我的 Excel 日期字段中有一个日期 16-06-2012。使用 PHP 导入时显示 41076。

任何人都可以帮忙吗?

4

4 回答 4

4

MS Excel 默认以 01-01-1900 为基础 您可以轻松地将 excel 整数日期值转换为 php 中的日期类型 请参阅

$intdatevalue=excel date value in integer

echo date('Y-m-d',strtotime('1899-12-31+'.($intdatevalue-1).' days'));

1899-12-31 因为 1900 年算作闰年。

它将解决您的excel日期导入问题

于 2012-07-17T09:23:01.000 回答
2
function ExcelToPHP($dateValue = 0, $ExcelBaseDate=0) {
    if ($ExcelBaseDate == 0) {
        $myExcelBaseDate = 25569;
        //  Adjust for the spurious 29-Feb-1900 (Day 60)
        if ($dateValue < 60) {
            --$myExcelBaseDate;
        }
    } else {
        $myExcelBaseDate = 24107;
    }

    // Perform conversion
    if ($dateValue >= 1) {
        $utcDays = $dateValue - $myExcelBaseDate;
        $returnValue = round($utcDays * 86400);
        if (($returnValue <= PHP_INT_MAX) && ($returnValue >= -PHP_INT_MAX)) {
            $returnValue = (integer) $returnValue;
        }
    } else {
        $hours = round($dateValue * 24);
        $mins = round($dateValue * 1440) - round($hours * 60);
        $secs = round($dateValue * 86400) - round($hours * 3600) - round($mins * 60);
        $returnValue = (integer) gmmktime($hours, $mins, $secs);
    }

    // Return
    return $returnValue;
}

传入:

your Excel date (e.g. 41076)
(optionally) a flag 0 or 4 to reflect the Excel base calendar.
    This is most likely to be 0

输出是 PHP 时间戳值

$excelDate = 41076;
$timestamp = ExcelToPHP($excelDate);
$mysqlDate = date('Y-m-d', $timestamp);

echo $mysqlDate, PHP_EOL;
于 2012-07-16T11:26:50.223 回答
0
$intdatevalue=excel date value in integer

echo date('Y-m-d',strtotime('1899-12-31+'.($intdatevalue-1).' days'));

这个答案是最好的。我什至不知道 Excel 的日期是 01-01-1900。所以我欠这个人很多。

我总是喜欢时间戳日期。

于 2013-10-08T14:05:44.390 回答
-2

Excel 中的日期使用自 Unix 纪元以来的天数存储。

您可能可以执行以下操作:

$excelDate = 41076;
$timestamp = $excelDate * 60 * 60 * 24;
$mysqlDate = date('Y-m-d', $timestamp);
于 2012-07-16T11:16:59.760 回答