我面临着一个问题,当导入到一个MySQL数据库的文件使用PHP。它为每个日期字段值显示一个整数值。
例如,假设我的Excel日期字段中有一个日期16-06-2012。用PHP导入时显示为41076。
有人能帮上忙吗?
发布于 2012-07-17 17:23:01
MS Excel默认采用01-01-1900为基数,您可以在php中轻松地将excel整型日期值转换为日期类型(请参阅
$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-16 19:26:50
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;发布于 2013-10-08 22:05:44
$intdatevalue=excel date value in integer
echo date('Y-m-d',strtotime('1899-12-31+'.($intdatevalue-1).' days'));这是最好的答案。我甚至不知道Excel的日期是从1900年1月1日开始。所以我欠这个人很多。
我总是喜欢时间戳日期。
https://stackoverflow.com/questions/11503051
复制相似问题