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


How do I convert a date from the format mm/dd/yy to dd/mm/yy in PHP now the function split() is deprecated?

好的,所以我花了一年时间从事网络开发来追求其他职业兴趣,然后本周回到它检查一个客户网站,该网站的发布已经暂停了一段时间,并发现由于我的托管公司更新了服务器上的 PHP 版本, 包含日期转换函数的脚本会抛出错误,因为 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();
        ?>

PHP 内置了一个惊人的日期/时间库。你最好使用它。

另外,请花时间了解SQL注入。您的代码非常容易受到最简单的攻击,因此请尽快修复它。

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

重读后听起来你想用preg_split('#/|-#' ...

你能不能不要使用:

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

编辑:

如所示,此线程:

转换日期格式 yyyy-mm-dd => dd-mm-yyyy