2012-05-15 24 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('/ -...',我*仍然*有警告 –

相关问题