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();
?>