I’m trying to display the time x started, the time x finished, and how long x took to complete. I have the start and end displaying correctly, but the following subtraction gives me a bonkers answer.
// to unix timestamps for subtraction
$startTime = strtotime($row['bp_rec_start']);
$endTime = strtotime($row['bp_rec_end']);
$timeTaken = $endTime - $startTime;
//back to date formats
$startTime = date('H:i',$startTime);
$endTime = date('H:i',$endTime);
$timeTaken = date('H:i',$timeTaken);
e.g. ( 01:24 – 01:23 = 07:01)
Thanks
Timestamps are seconds since 1970, each timestamp representing an absolute point in time. So
$endTime - $startTimeproduces some point in time like1975-04-12 07:01:52. Printing the hour and minute part of that will of course print07:01. The timestamp itself though is the difference in seconds, so you can do:You should of course look into
DateInterval(look at the 3rd example).