0

I have looked everywhere for a simple answer to this but I have not been able to find a solution that either works or meets my requirements.

I am looping through data, that looks similar to this:

Array
(
    [0] => 4c36fd909b37208b
    [1] => event:
    [2] => start_scan
    [3] => 2012-08-17 12:01:15
)
Array
(
    [0] => 4c36fd909b37208b
    [1] => sysaction:
    [2] => lower_device
    [3] => 2012-08-17 12:01:19
)
Array
(
    [0] => 4c36fd909b37208b
    [1] => event:
    [2] => how_to_use_displayed
    [3] => 2012-08-17 12:01:46
)
Array
(
    [0] => 4c36fd909b37208b
    [1] => event:
    [2] => scan_displayed
    [3] => 2012-08-17 12:01:59
)
Array
(
    [0] => 4c36fd909b37208b
    [1] => sysaction:
    [2] => layer_1_display_on_recognition
    [3] => 2012-08-17 12:02:23
)

I want to work out the time between two dates and the result needs to be in the format HH:MM:SS. In the example data above, I need the difference between 2012-08-17 12:01:15 and 2012-08-17 12:02:23, which should be 00:01:08.

I have tried to use the code from here: http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/ but it keeps returning 00:00:20

I have also tried this (where the date in the first array is saved as $xpTime and the date in the last array is set to $xpEndTimeTmp):

$start = new DateTime($xpTime);
$end = new DateTime($xpEndTimeTmp);
$diff = $start->diff($end);
$xpDuration = date('H:i:s', strtotime($diff->h.':'.$diff->i.':'.$diff->s));

But that seems to be working either (it returns the same hour, minute and a different second).

I hope someone can help me, tell me what I am doing wrong, or has sample code that could that could set me on the right track!

Ronny vdb
  • 2,324
  • 5
  • 32
  • 74
  • 1
    There was a similar question [function that formats the time difference](http://stackoverflow.com/questions/5343836/function-that-formats-the-time-difference) – Ibu Aug 17 '12 at 17:45
  • This might help: http://stackoverflow.com/questions/1762936/how-do-i-find-the-difference-between-a-datetime-type-and-the-current-date-time-i?rq=1 – Jim Aug 17 '12 at 17:45
  • 1
    `date("H:i:m", strtotime($a) - strtotime($b)`? – Matt Aug 17 '12 at 17:45
  • 1
    @Matt: That will fail for differences larger than 24 hours. It will only show the modulo of 24 hours. – Shi Aug 17 '12 at 17:47

3 Answers3

2
$t1 = '2012-08-17 12:01:15';
$t2 = '2012-08-17 12:02:23';

echo gmdate("H:i:s", strtotime($t2) - strtotime($t1));

Result

00:01:08
Kermit
  • 33,827
  • 13
  • 85
  • 121
  • incorrect... replace **$t2** with this line `$t2 = "2020-09-30 12:02:23";` and tell me why it still prints **00:01:08** even difference is more than **8 years**. – Wh1T3h4Ck5 Aug 17 '12 at 18:29
  • @Wh1T3h4Ck5 If he needs a greater than 24 hour difference I can change my answer. – Kermit Aug 17 '12 at 20:08
1

This version also works when the difference is more than 24 hours:

$start = strtotime('2012-08-17 12:01:15');
$end = strtotime('2012-08-17 12:02:23');
$delta = $end - $start;

$hours = floor($delta / 3600);
$remainder = $delta - $hours * 3600;
$formattedDelta = sprintf('%02d', $hours) . gmdate(':i:s', $remainder);
echo $formattedDelta;
Janis Elsts
  • 754
  • 3
  • 11
0

If you only want to know how to calculate the time-difference, there is no need to supply all that code in your question.
A simple: 'I need the difference between 2012-08-17 12:01:15 and 2012-08-18 12:02:23, which should be 24:01:08' would have been much clearer. In this case Janis Elsts' answer beat me to it :)

If however you put the example-code there for a reason, because you also want to know how to get the first and last time, you should look into multi-dimensional array's (combined with a working time-difference routine).

So assuming you have stuffed all your single array's in one wrapping array called $log, you would then do something like:

$timeDiff = strtotime(end($log)[3]) - strtotime(reset($log)[3]);

$hours = floor($timeDiff / 3600);
$remainder = $timeDiff - $hours * 3600;
$formattedTime = sprintf('%02d', $hours) . gmdate(':i:s', $remainder);
echo $formattedTime;
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80