2012-07-16 18 views
0

導入Excel文件到使用PHP MySQL數據庫,當我面臨的一個問題有些整數值。它爲每個日期字段值顯示一個整數值。PHP的Excel日期字段值,而進口在PHP的MySQL其給予

例如,假設有一個日期16-06-2012在我的Excel日期字段。它使用PHP導入時顯示41076。

任何人都可以幫忙嗎?

回答

-2

日期在Excel中使用自Unix紀元的天數被存儲。

你也許可以做這樣的事情:

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

不正確 - Excel的基準日期爲18-Dec-1899或1904年1月1日(取決於Windows或Mac日曆設置) - 這不是Unix紀元 – 2012-07-16 11:24:15

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; 
+0

當我嘗試將69704轉換爲MySQL日期時, 1954-09-26'而不是'2090-11-02'。 – Saurabh 2016-05-03 19:00:18

+1

@Saurabh - 那麼你可能運行的是PHP的32位版本,它的日期範圍在1901-12-13和2038-01-19之間....如果你需要使用這個日期之外的日期範圍,然後切換到使用64位版本的PHP – 2016-05-03 19:39:16

+0

感謝您的迴應。你能告訴我如何檢查PHP版本是32位還是64位? – Saurabh 2016-05-03 19:43:41

3

MS Excel的默認採用01 -01-1900基於 您可以輕鬆地轉換Excel整型日期值到Date類型在PHP 見

$intdatevalue=excel date value in integer 

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

1899年12月31日,因爲1900年算作閏年。

這將解決您的Excel日期導入問題

0
$intdatevalue=excel date value in integer 

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

這個答案是最好的。我甚至不知道Excel需要從01-01-1900的日期。所以我欠這個人很多。

我總是喜歡戳日期。