Could someone help me with this code? I need to compare the date completed(datecomp) to the date paid(datepaid) to establish the number of days difference(dayslate). Thanks in advance!

<?php
$stat = mysql_connect("localhost","root","");
$stat = mysql_select_db("oodb");
$query = "SELECT name FROM oocust Where ordernum='$ordernum'";
$stat = @mysql_fetch_assoc(mysql_query($query));
echo $stat["name"];
$result= mysql_query("select * from oocust WHERE pd=' '");
while($row=mysql_fetch_array($result))
{
$id=$row['id'];
$pd=$row['pd'];
$datecomp=$row['datecomp'];
$datepaid=$row['datepaid'];
$charges=$row['charges'];
$paidamt=$row['paidamt'];
$owed=$row['owed'];
$dayslate=$row['dayslate'];
$tax=$row['tax'];
$amtdue=$row['amtdue'];

// $dayslate = $datecomp - $datepaid;

 function number_of_days($date1, $date2)
 {  
 $date1Array = explode('/', $date1);  
 $date1Epoch = mktime(0, 0, 0, $date1Array[1],  
 $date1Array[0], $date1Array[2]);  
 $date2Array = explode('/', $date2);  
 $date2Epoch = mktime(0, 0, 0, $date2Array[1],  
 $date2Array[0], $date2Array[2]);  
 $dayslate = $date2Epoch - $date1Epoch;  
 return round($dayslate / 60 / 60 / 24);  
 }  
 echo number_of_days("04/7/2009", "12/7/2009");


 if ($paidamt == $amtdue)
{
$pd = 'P';
}
$tax = $charges * .06;
$owed = $charges + $tax + $shipamt - $paidamt;
$sql = "UPDATE oocust SET
pd='$pd', owed='$owed', dayslate='$dayslate', tax='$tax' 
WHERE id='$id'";
mysql_query($sql) ;
$err=mysql_error();
if($err!=""){
  echo "Error in $sql: $err\n";
}
}
echo "Records have been updated";
?>

Dani AI

Generated

— the fatal "Cannot redeclare" error comes from defining the same function more than once. and are right: declare the helper function once (outside the loop) or guard it with a check like if (!function_exists(...)) so PHP won’t try to redeclare it on each iteration.

A more robust approach: use PHP's DateTime rather than manual string parsing. DateTime handles leap years, different formats, and timezones more safely. Below is a compact helper you can place at the top of the script (once), then call inside the loop to set $dayslate. It returns a signed integer (positive when the second date is after the first) or null on invalid input.

function compute_days_between($dateCompleted, $datePaid) {
    try {
        $d1 = DateTime::createFromFormat('d/m/Y', $dateCompleted) ?: new DateTime($dateCompleted);
        $d2 = DateTime::createFromFormat('d/m/Y', $datePaid)     ?: new DateTime($datePaid);
    } catch (Exception $e) {
        return null;
    }
    $diff = $d2->diff($d1);
    return (int) $diff->format('%r%a');
}

Integration notes and quick fixes:

  • Put that function above your while loop (or in an included file loaded once). Call it inside the loop and assign the result to $dayslate before the UPDATE.
  • Validate empty or malformed dates and decide policy (NULL, 0, or skip update) so the DB stays consistent.
  • Avoid reusing one variable for different resources (e.g., don’t reuse $stat for connection, result, and array). Check query results and errors after each DB call.
  • Consider switching from deprecated mysql_* functions to mysqli or PDO with prepared statements to prevent SQL injection and improve reliability.

If you still see redeclare errors, search the codebase for other definitions of the same function (includes, requires, or multiple imports) or wrap the declaration in if (!function_exists('compute_days_between')) { ... }. Also clear any opcode cache after changing function names.

Recommended Answers

All 2 Replies

take your declaration of function number_of_days
outside your while loop
best is top of the script

Do not declare your function inside the loop. That will make your function declared as many as the loop iterate and php don't allow it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.