I am retrieving records from a database which have separate date and time field and combining them into date time objects so that I can get the difference between start and finish date/time. I also want to calculate to total number of hours that have passed for the number of records returned.
My current code is like this
$temp_total = date_create("00:00");
while($row = mysqli_fetch_assoc($result))
{
$startDate = $row['StartDate'];
$startTime = $row['StartTime'];
$start = $startDate . " " . $startTime;
$start = new DateTime($start);
$finishDate = $row['FinishDate'];
$finishTime = $row['FinishTime'];
$finish = $finishDate . " " . $finishTime;
$finish = new DateTime($finish);
$duration = $start->diff($finish);
$temp_total->add($duration);
}
This just ends up with the times wrapping around i.e. 8hr, 12hr and 12 hrs gives me a total of 8 hours which I assume is because I am missing the added day somewhere.
Am I going about this in the right way or is there a better way of doing it?