1

I have two times. I want to get time difference between seconds, suppose there are two time time1 = 4:20 and time2 =20:10 now i want to get difference in seconds between them .

i do not have date parameters here ,please do not mark post as duplicate : Getting time difference between two times in PHP

there there is day also , so my case is different

Community
  • 1
  • 1
mydeve
  • 553
  • 1
  • 14
  • 39
  • In what way is this different from the question you've included? – George Nov 09 '14 at 13:59
  • possible duplicate of [Getting time difference between two times in PHP](http://stackoverflow.com/questions/13928021/getting-time-difference-between-two-times-in-php) – George Nov 09 '14 at 14:02
  • What do you mean by that? You have two times in the form of strings that you want to find the difference of. That's the same as the question marked as duplicate. – George Nov 09 '14 at 14:03
  • there is day also: where? – KyleK Nov 09 '14 at 14:04

3 Answers3

2
$time1 = strtotime('01:30:00');
$time2 = strtotime('02:20:00');

$time_def = ($time2-$time1)/60;

echo 'Minutes:'.$time_def;
JustBaron
  • 2,319
  • 7
  • 25
  • 37
sohel shaikh
  • 102
  • 1
  • 12
0

If 4 in 4:20 is minutes and 20 is seconds:

function minutesAndSecondsToSeconds($minutesAndSeconds) {
    list($minutes, $seconds) = explode(':', $minutesAndSeconds);
    return $minutes * 60 + $seconds;
}

echo minutesAndSecondsToSeconds('20:10') - minutesAndSecondsToSeconds('4:20');

With hours:

function timeToSeconds($time) {
    list($hours, $minutes, $seconds) = explode(':', $time);
    return $hours * 3600 + $minutes * 60 + $seconds;
}

echo timeToSeconds('3:20:10') - timeToSeconds('1:20:00');

Or simply use strtotime that should works as explained here: Getting time difference between two times in PHP

Community
  • 1
  • 1
KyleK
  • 4,643
  • 17
  • 33
  • what ifi include hours also – mydeve Nov 09 '14 at 14:25
  • If include hours, so your problem is the same as http://stackoverflow.com/questions/13928021/getting-time-difference-between-two-times-in-php so you can use strtotime or the same logic as in my answer, just add $hours in list and $hours * 3600 in result. it's some very basic algorythm you can easily adapt if you try to understand this function. – KyleK Nov 09 '14 at 14:47
0

This function will work for MM:SS and HH:MM:SS format:

function TimeToSec($time) {
    $sec = 0;
    foreach (array_reverse(explode(':', $time)) as $k => $v) $sec += pow(60, $k) * $v;
    return $sec;
}

To calculate difference, use:

echo TimeToSec('20:10') - TimeToSec('4:20');

demo

Glavić
  • 42,781
  • 13
  • 77
  • 107