I have a mySQL table with 2 columns that contain a date. The while loop puts each of them into a variable: $start_date and $end_date, calculates the time between them and puts that into a new variable $since_start by using diff(); As far as I've understood using diff() results in a DateInterval Class.
Now I'd like to build a sum during the 'while' loop and store it in the $total_received variable. Last thing I tried, after searching the net and stackoverflow, was
$total_received->add(new DateInterval($since_start));
But that seems to be wrong, as I dont get any output. I don't get what I'm doing wrong, but I don't exactly know what Im doing with this line, to be honest and don't know where else to look. I wish I could find the answer by google, as that is much quicker, but I couldn't. I hope you can help!
Here's the full loop with the $total_received variable defined before and put out afterwards.
//Set variable for lifetime received total
$total_received = 0;
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo
$row["id"] ." "
. $row["donor"]." ";
$start_date = new DateTime($row["start"]);
$end_date = new DateTime($row["end"]);
$since_start = $start_date->diff($end_date);
$total_received->add(new DateInterval($since_start));
echo $since_start->format('%h')." Hours ".$since_start->format('%i')." Minutes "
. $row["subject"] ." "
. $row["complete"] ."<br>";
}
} else {
echo "No lifetime received yet";
}
echo $total_received->format('%h')." Hours ".$total_received->format('%i')." Minutes ";
Thank you very much in advance!