CoderZone.org

Category: >> PHP Code >> Calculate the difference between two times formatted in HH:MM Bookmark and Share

<< lastnext >>

Snippet Name: Calculate the difference between two times formatted in HH:MM

Description: A script that calculates the difference between two times, formatted in HH:MM. For example, if you enter 23:40 and 01:30, it will calculate it as a time difference of "01:50".

Also see:
» Convert Minutes to Days, Hours, an...
» LDAP Timestamp Converter
» Time left In Weeks, Days, Hours, A...
» Get Relative Dates and Times
» Convert Seconds to Hours:Minutes:S...
» %TYPE vs %ROWTYPE - What's the dif...
» Convert Minutes to Hours
» Convert Hours to Minutes
» Convert 12 to 24 hour time and vic...
» Calculate time difference between ...
» Show Runtime in days and hours
» Days in month #2
» Days in month #1
» Convert minutes to hours #2
» Convert minutes to hours #1

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: May 14th, 2010

<?php
 
echo getTimeDiff("23:30","01:30");
 
function getTimeDiff($dtime,$atime){
 
 $nextDay=$dtime>$atime?1:0;
 $dep=explode(':',$dtime);
 $arr=explode(':',$atime);
 $diff=abs(mktime($dep[0],$dep[1],0,date('n'),date('j'),date('y'))-mktime($arr[0],$arr[1],0,date('n'),date('j')+$nextDay,date('y')));
 $hours=floor($diff/(60*60));
 $mins=floor(($diff-($hours*60*60))/(60));
 $secs=floor(($diff-(($hours*60*60)+($mins*60))));
 if(strlen($hours)<2){$hours="0".$hours;}
 if(strlen($mins)<2){$mins="0".$mins;}
 if(strlen($secs)<2){$secs="0".$secs;}
 return $hours.':'.$mins.':'.$secs;
}
 
?> 
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 8