【问题标题】: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 中将日期从 mm/dd/yy 格式转换为 dd/mm/yy 函数 split() 已弃用?
【发布时间】:2012-05-15 22:14:27
【问题描述】:

好的,所以我休了一年的网络开发来追求其他职业兴趣,然后这周回来检查一个客户网站,它的发布已经暂停了一段时间,一切准备就绪,发现因为我的托管公司更新了服务器上的 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();
        ?>

【问题讨论】:

  • 不相关,但你有一些不错的 SQL 注入漏洞。

标签: php string date split deprecated


【解决方案1】:

PHP 有一个惊人的 Date/Time library 内置。你最好使用它。

另外,请花时间了解SQL Injection。你的代码很容易受到最简单的攻击,所以fix it as soon as possible

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 正如我在问题中所说,我已经尝试过explode 和preg_split,并且都导致了警告和错误。 Explode 导致未定义偏移量的警告,以“//--16/05/2012”格式返回日期,更严重的是,从显示的列表中删除记录(我还没有检查它对数据库的影响本身)。 Preg_split 导致两个未定义偏移量的相同警告,并再次弄乱返回的日期并从显示的记录列表中删除记录。显然我缺少一些东西,但我不知道是什么。
    • @NeonBlueBliss 你有输入字符串的例子吗?我尝试使用list(... = split('/-...,但我仍然收到警告
    【解决方案3】:

    你不能用吗:

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

    编辑:

    正如所见是这个线程:

    Convert date format yyyy-mm-dd => dd-mm-yyyy

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-25
      • 2021-12-31
      • 1970-01-01
      • 2022-11-19
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      相关资源
      最近更新 更多