Skip to content

Instantly share code, notes, and snippets.

@alhoseany
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alhoseany/d11b9bab4642a5db022b to your computer and use it in GitHub Desktop.
Save alhoseany/d11b9bab4642a5db022b to your computer and use it in GitHub Desktop.
get time difference in milliseconds
function find_time_diff($t1, $t2, $ms=false) {
$time1_pcs = explode(":", $t1);
$time1_pcs2 = explode(".", $time1_pcs[2]);
$time2_pcs = explode(":", $t2);
$time2_pcs2 = explode(".", $time2_pcs[2]);
$time1_all_ms = ($time1_pcs[0] * 60 * 60 * 1000) + ($time1_pcs[1] * 60 * 1000) + ($time1_pcs2[0] * 1000) + $time1_pcs2[1];
$time2_all_ms = ($time2_pcs[0] * 60 * 60 * 1000) + ($time2_pcs[1] * 60 * 1000) + ($time2_pcs2[0] * 1000) + $time2_pcs2[1];
$time1_all_ms_dif = $time2_all_ms - $time1_all_ms;
if($ms){
return $time1_all_ms_dif;
}else{
$dif_ms = $time1_all_ms_dif % 1000;
$time1_all_ms_dif = floor($time1_all_ms_dif / 1000);
$dif_s = $time1_all_ms_dif % 60;
$time1_all_ms_dif = floor($time1_all_ms_dif / 60);
$dif_m = $time1_all_ms_dif % 60;
$time1_all_ms_dif = floor($time1_all_ms_dif / 60);
$dif_h = $time1_all_ms_dif % 60;
return $dif_h.':'.$dif_m.':'.$dif_s.'.'.$dif_ms;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment