Php 如何将excel中的时间戳格式值插入数据库
我想将excel中的时间戳格式值插入数据库。该值有点像“2016年1月6日12:00:00 AM”。我使用此代码将值插入数据库Php 如何将excel中的时间戳格式值插入数据库,php,mysql,excel,timestamp,Php,Mysql,Excel,Timestamp,我想将excel中的时间戳格式值插入数据库。该值有点像“2016年1月6日12:00:00 AM”。我使用此代码将值插入数据库 $TimeStamp = mysqli_real_escape_string($conn,trim($objPHPExcel->getActiveSheet()->getCell('A' . $row)->getValue())); 但结果是这个值是“42522”。似乎该值的传输不正确。代码似乎有错误,不适合使用插入时间戳格式值。我可以知道如何纠正这
$TimeStamp = mysqli_real_escape_string($conn,trim($objPHPExcel->getActiveSheet()->getCell('A' . $row)->getValue()));
但结果是这个值是“42522”。似乎该值的传输不正确。代码似乎有错误,不适合使用插入时间戳格式值。我可以知道如何纠正这个吗
多谢各位 Excel在内部将日期存储为自1900年1月1日以来的天数。 例如:“2011年6月9日上午10:30”将存储为“40703.4375”。 40703是自1900年1月1日起的完整天数,0.4375表示时间(10.5/24=0.4375) 当您处理从Excel电子表格读取的日期时(例如,使用PHPExcel),您通常希望将其转换为UNIX时间戳,即自UTC 1970年1月1日午夜以来经过的秒数 下面是一段PHP代码:
// Numbers of days between January 1, 1900 and 1970 (including 19 leap years)
define("MIN_DATES_DIFF", 25569);
// Numbers of second in a day:
define("SEC_IN_DAY", 86400);
function excel2timestamp($excelDate)
{
if ($excelDate <= MIN_DATES_DIFF)
return 0;
return ($excelDate - MIN_DATES_DIFF) * SEC_IN_DAY;
}
//1900年1月1日至1970年(包括19闰年)之间的天数
定义(“最小日期差异”,25569);
//一天中的秒数:
定义(“第二天”,86400);
函数excel2timestamp($excelDate)
{
如果($excelDateExcel为您提供了正确的信息,并且您的操作也正确。为了澄清这一点,Excel以整数格式存储时间戳值。因此,这里您得到的值“42552”显然是正确的,现在您必须使用此函数来实现您正在执行的操作
<?php
function ExcelToTimeStamp($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;
}
$myTimestamp = // Output will be 146473920001 and it is a timestamp in php you can store it
echo date('d M Y', $myTimestamp); now convert that timestamp value in any format you want usnig date()
?>
DB表中的列类型是什么?我将其命名为Timestamp并声明为varchar(25)。我使用了这个“$DateTime2=date($Y-m-d H:I:s',strottime($DateTime));$DateTime2=excel2timestamp($DateTime2);'代码,但返回0。我可以知道原因吗?excel无法编辑,因为它来自第三方。无法编辑。我可以知道如何在代码中实现这一点吗?