在PHP-mysql中导入时,PHP Excel日期字段值给出了一些整数值


PHP Excel Date field value while importing in php mysql its giving some integer value

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

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

有人能帮忙吗?

MS Excel默认日期为01-01-1900您可以在php中轻松地将excel整数值转换为date类型参见

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

1899-12-31年,因为1900年是闰年。

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

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;
$intdatevalue=excel date value in integer
echo date('Y-m-d',strtotime('1899-12-31+'.($intdatevalue-1).' days'));

这个答案是最好的。我甚至不知道Excel的日期是2000年1月1日。所以我欠这个人很多。

我总是喜欢时间戳日期。

Excel中的日期使用自Unix epoch以来的天数进行存储。

你可能可以这样做:

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