0

How do I add two double numbers in PHP? I have two dates that I compare with each other and that works fine ($uren is the variable for that). But now I want to sum all those ($uren) from the while so I can make a week sum ($weekuren is the variable for that) from the separate hours ($uren). The problem is that it don't want to add. In the while are the hours ($uren) visible but they don't add. The numbers are double like 3.98 for example. What do I wrong?

$sq = mysql_query("SELECT * FROM presentielijst  ORDER BY datum ASC") or die(mysql_error());
        
$tel = mysql_num_rows($sq);

echo "<table cellpadding='5' cellspacing='2' border='0' width='100%' style='font-size:14pt'>";

    while($d = mysql_fetch_assoc($sq)) {
        
        $p_id = stripslashes($d['p_id']);
        $weeknummer = stripslashes($d['weeknummer']);
        $dag = stripslashes($d['dag']);
        $datum = stripslashes($d['datum']);
        $weekuren = stripslashes($d['weekuren']);
        $toelichting = stripslashes($d['toelichting']);
        $kleur = stripslashes($d['kleur']);
        $aanmeldtijd = strtotime($d['aanmeldtijd']);
        $afmeldtijd = strtotime($d['afmeldtijd']);

        $uren = abs($afmeldtijd - $aanmeldtijd) / 3600;  //row value 1: 6.17 //row value 2: 3.980

        $uren = round($uren, 2);

        $weekuren = $uren += $uren;

        echo "<td class='presentieurentd' width='380px' bgcolor='".$kleur."' onclick=open_gegevens('".$p_id."')>".$uren."</td>";
}
echo $weekuren;  //value 7.96? Strange value based on I don't know
echo "</table>";

Thanks in advance Edit: the values I want to add are 6.1666666667 + 3.983611111

17
  • 1
    $weekuren = $uren * 2;
    – Cid
    Sep 9, 2022 at 12:43
  • What happens when you run the code? What have you tried to resolve the problem?
    – Nico Haase
    Sep 9, 2022 at 12:44
  • $weekuren += $uren;? Sep 9, 2022 at 12:45
  • 2
    Sidenote: if this code works with the use of mysql_fetch_assoc() you’re using a seriously outdated version of PHP or a polyfill that was intended to be a temporary stopgap until you migrate your code. That function no longer exists. Sep 9, 2022 at 12:47
  • Can you provide examples of what the variable values are and what the expected output should be? Sep 9, 2022 at 12:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.