在PHP中,如何将日期从mm/dd/yy格式转换为dd/mm/yy现在函数split()已被弃用?

在PHP中,如何将日期从mm/dd/yy格式转换为dd/mm/yy现在函数split()已被弃用?,php,string,date,split,deprecated,Php,String,Date,Split,Deprecated,好的,所以我花了一年的时间从web开发中寻求其他职业兴趣,然后在本周回来检查一个客户网站,该网站的发布已经暂停了一段时间,现在一切都准备就绪,并且发现由于我的托管公司更新了服务器上的PHP版本,包含日期转换函数的脚本正在抛出错误,因为split()已被弃用(这将教会我如何抽出一年时间!) 在环顾这里和其他地方之后,我读到了各种各样的文章,我应该使用preg_split()、explode()和strotime()。理论上听起来很简单,但当我尝试它们时,要么日期格式错误(例如/--15/5/12)

好的,所以我花了一年的时间从web开发中寻求其他职业兴趣,然后在本周回来检查一个客户网站,该网站的发布已经暂停了一段时间,现在一切都准备就绪,并且发现由于我的托管公司更新了服务器上的PHP版本,包含日期转换函数的脚本正在抛出错误,因为split()已被弃用(这将教会我如何抽出一年时间!)

在环顾这里和其他地方之后,我读到了各种各样的文章,我应该使用preg_split()、explode()和strotime()。理论上听起来很简单,但当我尝试它们时,要么日期格式错误(例如/--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();
        ?>

split
已弃用,但
explode
未弃用。它也做了同样的事情,但使用的是字符串而不是表达式(如果
split
这样做了,我实际上不确定)。当然,总是有
preg\u split

重读后,听起来您想使用
preg_split(“#/|-#”…

您不能使用:

$date = "01/12/2012"; 
echo date("dd/mm/yyyy", strtotime($date)); 
编辑:

如图所示,该线程:

PHP有一个惊人的内置功能。你最好使用它


另外,请花点时间了解。您的代码非常容易受到最简单的攻击,因此。

不相关,但您有一些很好的SQL注入漏洞。正如我在问题中所说,我尝试过explode和preg_split,两者都会导致警告和错误。explode会导致未定义偏移量的警告,returns格式为“/--16/05/2012”的日期,更严重的是,从显示的列表中删除记录(我还没有检查它对数据库本身的影响).Preg_split会对两个未定义的偏移量发出相同的警告,并再次将返回的日期弄乱,并将记录从显示的记录列表中删除。显然,我缺少一些内容,但我不知道是什么。@NeonBlueBliss您有输入字符串的示例吗?我尝试使用
list(…=split)('/-…
我仍然收到警告