2012-05-15 23 views
0

好吧,我花了一年的時間從​​網絡開發中尋求其他職業興趣,然後在本週回來查看一個客戶端的網站,這個網站已經暫停發佈了一段時間了,他們都準備好了,並且發現因爲我的託管公司更新了服務器上的PHP版本,所以包含日期轉換函數的腳本會拋出錯誤,因爲split()已被棄用(這會教我花一年時間!)。如何在PHP中將日期從格式mm/dd/yy轉換爲dd/mm/yy現在函數split()已棄用?

環顧這裏和其他地方後,我已經不同地閱讀了我應該使用preg_split(),explode()和strtotime()。理論上聽起來很容易,但是當我嘗試過它們時,要麼出現格式錯誤的日期(例如/ - 15/5/12),我得到未定義的偏移警告,或腳本似乎工作,但打印的確認更新的工作顯示可怕的1-1-70日期,所以現在不僅我的腳本不工作,但我的數據庫有一些奇怪的空白條目,並奇怪地增加總數(至少數據庫是相對容易找到並修復錯誤 - 一旦我記得我把服務器密碼放在哪裏!)。

我知道我可以隱藏split()錯誤,但這不是最佳實踐,我想嘗試讓它按照它的方式工作。有問題的腳本在下面。我想我的問題的簡短版本是,我需要做什麼來重寫這個腳本,以便它再次工作?

 <?php 
     require_once('/home/thebooks/admins/connect.php'); 
     $id = stripslashes($_POST['id']);    
     $date = stripslashes($_POST['dateinput']); // get the data from the form 
     $amountraised = stripslashes($_POST['amountraised']); 
     $comments = stripslashes($_POST['comments']); 
     // *** function dateconvert *** 
     // dateconvert converts data from a variable (posted from a form or just stored) 
     // into a format mysql will be able to store and converting the 
     // database date back into the british standard of date month year. 
     // The script accepts day.month.year or day/month/year or day-month-year. 
     // @param string $date - Date to be converted 
     // @param string $func - which function is to be used (1 for input to mysql, 2 for output from mysql)  
     require_once('/home/thebooks/admins/connect.php'); 
     $id = stripslashes($_POST['id']);    
     $date = stripslashes($_POST['dateinput']); // get the data from the form 
     $amountraised = stripslashes($_POST['amountraised']); 
     $comments = stripslashes($_POST['comments']); 
     // using type 1 
     $date = dateconvert($date,1); // Would convert to e.g. 2005-12-19 which is the format stored by mysql 
     function dateconvert($date,$func) { 
     if ($func == 1){ //insert conversion 
     list($day, $month, $year) = split('/-', $date); 
     $date = "$year-$month-$day"; 
     return $date; 
     } 
     if ($func == 2){ //output conversion 
     list($year, $month, $day) = split('/-', $date); 
     $date = "$day/$month/$year"; 
     return $date; 
      } 
     } 
     $update = "UPDATE fundraisingtotal SET date = '$date', amountraised = '$amountraised', comments = '$comments' WHERE id='$id' "; 
     $result = mysql_query($update) or die(mysql_error()); 
     $realdate = dateconvert($date,2); // convert date to British date 
     if ($result) { 
     echo "<p class=\"dbpara\">Thank you. Your update to the record was successful.</p>"; 
     echo "<p class=\"dbpara\">The record has been amended to a date of <b>$realdate</b> and amount of <b>$amountraised</b>. Comments: <b>$comments</b></p>"; 
     } 
     else { 
     echo "<p>Nothing has been changed.</p>"; 
     } 
     mysql_close(); 
     ?> 
+3

不相關,但是你有一些很好的SQL注入漏洞。 –

回答

0

split已棄用,但explode不適用。它做同樣的事情,但用字符串而不是表達式(如果split這樣做 - 我其實不確定)。當然,總是有preg_split

重讀之後,它聽起來像你想使用preg_split('#/|-#' ...

+0

正如我在我的問題中所說的,我已經嘗試過explode和preg_split,並且都導致警告和錯誤。爆炸會導致未定義偏移的警告,以'// - 16/05/2012'格式返回日期,更嚴重的是,從顯示的列表中刪除記錄(我還沒有檢查過它對數據庫有什麼影響本身)。 Preg_split會導致兩個未定義的偏移相同的警告,並再次混淆返回的日期並從顯示的記錄列表中刪除記錄。顯然有一些我錯過了,但我不知道是什麼。 – BlissC

+0

@NeonBlueBliss你有一個輸入字符串的例子嗎?我嘗試使用'list(... = split('/ -...',我*仍然*有警告 –

相關問題