0

Okay, so I took a year out of web development to pursue other career interests, and then came back to it this week to check a client site which's launch has been on hold for some time is all set to go, and found that since my hosting company updated the PHP version on the server, that a script that includes a date conversion function is throwing errors because split() is deprecated (that'll teach me to take a year out!).

After looking around here and other places, I've variously read that I should be using preg_split(), explode() and strtotime(). Sound easy in theory, but when I've tried them, either the date comes out formatted wrong (e.g. /--15/5/12), I get an undefined offset warning, or the script seems to work but the printed confirmation that the update's worked shows the dreaded 1-1-70 date, so now not only does my script not work, but my database has some weird blank entries and is adding totals up strangely (at least the database is relatively easy to find and fix the errors - once I remember where I put my server password anyways!).

I know I can hide the split() error, but that's not best practice and I want to try and get it working as it should. The script in question is below. I guess the short version of my question is, what do I need to do to rewrite this script so it works again?

        <?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();
        ?>
kapa
  • 77,694
  • 21
  • 158
  • 175
BlissC
  • 841
  • 3
  • 14
  • 18

3 Answers3

1

PHP has an amazing Date/Time library built-in. You'd be better off using that.

Also, please take the time and learn about SQL Injection. Your code is very vulnerable to the simplest attacks, so fix it as soon as possible.

Community
  • 1
  • 1
kapa
  • 77,694
  • 21
  • 158
  • 175
0

split is deprecated, but explode is not. It does the same thing, but with a string rather than an expression (if split did that -- I'm actually not sure). Of course, there's always preg_split.

After rereading it sounds like you want to use preg_split('#/|-#' ...

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • As I said in my question, I've tried both explode and preg_split, and both are causing warnings and errors. Explode causes warnings of undefined offset, returns the date in the format '//--16/05/2012', and more seriously, deletes the record from the displayed list (I've not checked yet what effect it's had on the database itself). Preg_split causes the same warnings of two undefined offsets, and again messes up the returned date and removes the record from the displayed list of records. There's obviously something I'm missing, but I've no idea what. – BlissC May 15 '12 at 22:56
  • @NeonBlueBliss do you have an example of an input string? I tried using `list(... = split('/-...` and I *still* got warnings – Explosion Pills May 15 '12 at 22:57
0

Could you not use:

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

Edit:

As seen is this thread:

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

Community
  • 1
  • 1
Sharpedges
  • 405
  • 1
  • 4
  • 17