Reputation: 563
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
Upvotes: 1
Views: 2811
Reputation: 102
$time1 = strtotime('01:30:00');
$time2 = strtotime('02:20:00');
$time_def = ($time2-$time1)/60;
echo 'Minutes:'.$time_def;
Upvotes: 2
Reputation: 5131
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
Upvotes: 0
Reputation: 43582
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');
Upvotes: 0