2011-03-31 43 views
-1

可能重複比較:
How to convert date from one format to another如何兩個日期從數據庫中它得到的記錄從Excel

我有5列的電子表格。其中之一包含格式爲02-Dec-10的日期列。

當我喂的是Excel表單數據庫它存儲的空日期0000-00-00 0點零零分00秒

我如何可以將其存儲爲日期。 PLZ幫助

編輯1:

這是代碼使用養活數據數據庫CSV文件

 $date   =   date("d-m-Y");  
     $databasetable = "sample"; 
     $fieldseparator = ","; 
     $lineseparator = "\n"; 
     $csvfile = "../uploads/" . basename($_FILES['file']['name']); 
     $addauto = 0; 
     $save = 1; 
     $outputfile = "../temp/output.sql"; 
     if(!file_exists($csvfile)) { 
      #echo $csvfile; 
      echo "File not found. Make sure you specified the correct path.\n"; 
      return 0; 
      exit; 
     } 

     $file = fopen($csvfile,"r"); 

     if(!$file) { 
      #echo "Error opening data file.\n"; 
      return 0; 
      exit; 
     } 

     $size = filesize($csvfile); 

     if(!$size) { 
      echo "File is empty.\n"; 
      return 0; 
      exit; 
     } 

     $csvcontent = fread($file,$size); 

     fclose($file); 

     $lines = 0; 
     $queries = ""; 
     $linearray = array(); 

     foreach(split($lineseparator,$csvcontent) as $line) { 
      $lines++; 
      $line = trim($line," \t"); 
      $line = str_replace("\r","",$line); 
      $line = str_replace("'","\'",$line); 
      $linearray = explode($fieldseparator,$line); 
      $linemysql = implode("','",$linearray); 
      if($addauto) 
       $query = "insert into $databasetable values('','$linemysql');"; 
      else 
       $query = "insert into $databasetable values('$linemysql');"; 
      $queries .= $query . "\n"; 
      @mysql_query($query); 
     } 

     @mysql_close($con); 

     if($save) { 
       $file2 = fopen($outputfile,"w");      
       if(!$file2) { 
        echo "Error writing to the output file.\n"; 
        return 0; 
       } 
       else { 
        fwrite($file2,$queries); 
        fclose($file2); 
        return 1; 
       } 
     } 
     //echo "Found a total of $lines records in this csv file.\n"; 
+0

你在使用什麼樣的方法使用哪種類型的庫使用代碼來進入什麼樣的數據庫? – 2011-03-31 19:30:23

+1

請停止發佈重複內容,特別是如果新內容與其他內容不太一樣,並且已經提供了大量幫助。 – 2011-03-31 19:31:19

+0

這不是重複的隊友......我錯誤地發佈了這些問題。這個問題是無關的 – Rajasekar 2011-03-31 19:33:08

回答

0

從我的崗位適應您的其他類似的問題:

對於一個更一般的方法,你總是可以將你當前的格式轉換爲一個字符串,就像你擁有它,並且使用字符串操作來進行子串和重組。我知道一個事實,即MySQL接受DATETIME字段的字符串值。

$day = substr($input, 0, 2); 
$month = substr($input, 2, 3); 
switch($month){ 
    case "Jan": 
     $month = "01"; 
     break; 
    ... 
} 

插入這個字符串到你的數據庫作爲其顯示格式

0

只要你與日期1970年和2038年的工作而已,strtotime()應該是安全的使用。做到這一點每個日期列將其轉換爲MySQL的YYYY-MM-DD日期格式:

$column = date("Y-m-d", strtotime($column)); // Will return 2010-12-02 

確定一個日期列是手動指定它們的最簡單方法。否則,您必須事先獲取表格結構,並查看哪些列是DATEDATETIME

相關問題