1

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!

Wu Wei
  • 1,827
  • 1
  • 15
  • 27

1 Answers1

0

The problem is that:

  • $total_received = 0 initialises that variable as a number, which does not have the add method that you use later on.
  • $since_start is already a DateInterval, so doing new DateInterval($since_start) does not make much sense, and would trigger an error

You cannot add date intervals together, but only add date intervals to a date/time. So use some reference date/time, and add each interval to that. At the end you take the diff of the reference date/time with that result date/time to get the final interval:

// Create the "interval" as a start/end reference date
$ref_start = new DateTime("00:00");
$ref_end = clone $ref_start;

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);
        // Move the end date/time of the reference period
        $ref_end->add($since_start);
        echo $since_start->format('%h')." Hours ".$since_start->format('%i')." Minutes "
        . $row["subject"] ." " 
        . $row["complete"] ."<br>";
    }
} else {
    echo "No lifetime received yet";
}
// Only now convert the reference period to an interval
$total_received = $ref_start->diff($ref_end);

echo $total_received->format('%h')." Hours ".$total_received->format('%i')." Minutes ";
trincot
  • 317,000
  • 35
  • 244
  • 286