好吧,我花了一年的時間從網絡開發中尋求其他職業興趣,然後在本週回來查看一個客戶端的網站,這個網站已經暫停發佈了一段時間了,他們都準備好了,並且發現因爲我的託管公司更新了服務器上的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();
?>
不相關,但是你有一些很好的SQL注入漏洞。 –