好吧,我花了一年的时间从网络开发中寻求其他职业兴趣,然后在本周回来查看一个客户端的网站,这个网站已经暂停发布了一段时间了,他们都准备好了,并且发现因为我的托管公司更新了服务器上的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注入漏洞。 –