You can use DateInterval to correctly calculate the difference.
If I understand correctly, $row[4] contains the total number of seconds for breaks.
$row = [ '2' => '2017-02-06 08:00:00', '3' => '2017-02-06 18:10:00', '4' => 3660 ]; $dateOn = new \DateTime($row['2']); $dateOff = new \DateTime($row['3']); $delays = (int)$row['4']; $dateOn->add(new DateInterval('PT'.$delays.'S')); $time = $dateOff->diff($dateOn); echo $time->format('%H:%I:%S'); // 09:09:00
$row = [ '2' => '06:25:00', '3' => '07:00:00', '4' => '00:10:00' ]; $dateOn = new \DateTime($row['2']); $dateOff = new \DateTime($row['3']); if ($dateOn > $dateOff) { $dateOff->add(DateInterval::createFromDateString('1 day')); } if ($row['4']) { list($h, $i, $s) = explode(':', $row[4]); $dateOn->add(new DateInterval("PT{$h}H{$i}M{$s}S")); } $time = $dateOff->diff($dateOn); echo $time->format('%H:%I:%S'); // 00:25:00
This format is very unfortunate:
- The interval is stored as formatted time, it is easier to store it simply as the number of seconds.
- There are no dates where they are needed. It is necessary to manually check the fact of changing dates. If someone wants to work more than a day, the result will be incorrect.
strtotimebut just as seconds from the total time value - Moonvvell