Skip to content

Instantly share code, notes, and snippets.

@petk
Created May 10, 2018 16:31
Show Gist options
  • Save petk/8792ddff820ce1722feb8542ab624aa6 to your computer and use it in GitHub Desktop.
Save petk/8792ddff820ce1722feb8542ab624aa6 to your computer and use it in GitHub Desktop.
PHP example of DateInterval difference
<?php
function getDiff($start, $end)
{
$start = explode(':', $start);
$end = explode(':', $end);
$start = new DateInterval('PT'.$start[0].'H'.$start[1].'M'.$start[2].'S');
$end = new DateInterval('PT'.$end[0].'H'.$end[1].'M'.$end[2].'S');
$startTime = new DateTime();
$startTime->add($start);
$endTime = new DateTime();
$endTime->add($end);
$diff = $endTime->diff($startTime);
return $diff->format('%H:%I:%S');
}
var_dump(getDiff('178:10:28', '182:00:00'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment