I've got a bit of PHP code here that calculates (and echos out) a time duration. I have an if/else that chooses how to calculate the duration. Due to the way i have done the calculations, under one circumstance the duration that is output is a DateTime object, under the other it is a DateInterval object. The formatting of both is done outside the if/else statement.
The calculations are working fine, and the formatting for the DateTime object are working fine, but the formatting for the DateInterval object is off. It shows percent signs within the output time (example at the end of the codeblock).
$time1 = new DateTime($row[0]);
$time2 = new DateTime($row[1]);
if ($time1 > $time2) {
$twentyFourHours = new DateTime('240000');
$difference = $time1->diff($twentyFourHours);
$time2->add($difference);
$duration = $time2; // this is a DateInterval object
}
else {
$duration = $time2->diff($time1); // This is a DateTime object
}
echo $duration->format('%H:%I');
echo '<br>';
echo $row['2'];
}
Below is an example of the output i am getting (every other duration is the DateInterval object with the percent signs):
02:10
2016-06-16
%12:%0
2016-06-16
03:04
2016-06-17
%12:%0
2016-06-17
From what i can tell in the DateInterval formatting docs, i am setting the formatting correctly for two digit hours and two digit minutes ('%H:%I'), but the output would seem to prove otherwise. I am guessing i am overlooking something silly and would be grateful if someone could point out where i am going wrong.
Many Thanks!