Home » Php » PHP: Work out duration between two times

PHP: Work out duration between two times

Posted by: admin July 12, 2020 Leave a comment

Questions:

Ok, a really simple question but I am too thick to figure it out. I want to get the difference between two times. For example, “1:07” (1 minute and 7 seconds) and “3:01” (3 minutes and 1 second). It will only be ever minutes and seconds. I have been trying to make use of this:

function timeDiff($firstTime,$lastTime)
{

// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);

// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;

// return the difference
return $timeDiff;
}

But I think I am running in the wrong direction?

Thank you for any help.

EDIT

I tried this: echo timeDiff(‘1:07′,’2:30’);

I got this output “4980”

What is the above? Is it seconds? I have no idea how to get it as “1:23” which is the difference.

EDIT 2

Thank you all, I learnt so much just from this one thread, esp. Paul’s. It works very well and I like the defensiveness!

How to&Answers:

You can’t use strtotime as it will interpret MM:SS as HH:MM – that’s why you are getting higher values than expected.

You could simply prepend your MM:SS values with ’00:’ to make them look like HH:MM:SS.

Note however that strtotime, if just given HH:MM:SS, will give a timestamp for today, which is fine for throwaway code. Don’t use that technique for anything important, consider what happens if your two calls to strtotime straddle midnight!

Alternatively, something like this will turn a MM:SS value into a timestamp you can do arithmetic on

function MinSecToSeconds($minsec)
{
    if (preg_match('/^(\d+):(\d+)$/', $minsec, $matches))
    {
        return $matches[1]*60 + $matches[2];
    }
    else
    {
        trigger_error("MinSecToSeconds: Bad time format $minsec", E_USER_ERROR);
        return 0;
    }
}

It’s a little more defensive than using explode, but shows another approach!

Answer:

This should give you the difference between the two times in seconds.

$firstTime = '1:07';
$secondTime = '3:01';

list($firstMinutes, $firstSeconds) = explode(':', $firstTime);
list($secondMinutes, $secondSeconds) = explode(':', $secondTime);

$firstSeconds += ($firstMinutes * 60);
$secondSeconds += ($secondMinutes * 60);
$difference = $secondSeconds - $firstSeconds;

Answer:

Using code found here, how about calculating the seconds yourself:

<?php

function time_to_sec($time) {
    $hours = substr($time, 0, -6);
    $minutes = substr($time, -5, 2);
    $seconds = substr($time, -2);

    return $hours * 3600 + $minutes * 60 + $seconds;
}

function sec_to_time($seconds) {
    $hours = floor($seconds / 3600);
    $minutes = floor($seconds % 3600 / 60);
    $seconds = $seconds % 60;

    return sprintf("%d:%02d:%02d", $hours, $minutes, $seconds);
} 


function timeDiff($firstTime,$lastTime)
{

// convert to seconds
$firstTime=time_to_sec($firstTime);
$lastTime=time_to_sec($lastTime);

// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;

// return the difference
return sec_to_time($timeDiff);
}

echo timeDiff("1:07", "3:01");


?>

returns 0:01:54

Answer:

The strtotime function is for converting dates (down to the second) in a string format into a Unix Timestamp style format. It is NOT for converting strings in a time format into anything. See if these two statments help your undersatnding any

//recent php5 for DATE_RFC822
echo date(DATE_RFC822,strtotime('1:07'));
echo date(DATE_RFC822,strtotime('3:01'));   

It sounds like you want something like this

function timeDiff($firstTime,$lastTime)
{
    //split minutes and seconds
    list($min_one, $sec_one) = preg_split('{:}',$firstTime,2, PREG_SPLIT_NO_EMPTY);
    list($min_two, $sec_two) = preg_split('{:}',$lastTime ,2, PREG_SPLIT_NO_EMPTY);

    //convert times into seconds
    $time_one = $min_one * 60 + $sec_one;
    $time_two = $min_two * 60 + $sec_two;

    //return times
    return $time_two - $time_one;
}

echo timeDiff('1:07','3:10');

Answer:

$time1 = date("H:i",strtotime($strtime));
$time2 = date("H:i");
list($hours, $minutes) = explode(':', $time1);
$startTimestamp = mktime($hours, $minutes);

list($hours, $minutes) = explode(':', $time2);
$endTimestamp = mktime($hours, $minutes);

$seconds = $endTimestamp - $startTimestamp;
$minutes = ($seconds / 60) % 60;
$hours = floor($seconds / (60 * 60));