Find the duration in hours and minutes of two times using PHP

Another function that you can use to find the duration of any given two times. If you know PHP you can modify this further to include years, days & months (if you need help let me know!). But right now it will only say the time differences of two inputted times, which is actually what I need for now!

function duration($start,$end){
$tmp_e = explode(“:”,$end);
$tmp_s = explode(“:”,$start);
$start_time = mktime($tmp_s[0],$tmp_s[1],$tmp_s[2],8,8,8);
$end_time = mktime($tmp_e[0],$tmp_e[1],$tmp_e[2],8,8,8);
$durationt = $end_time-$start_time;
$hrs = intval(floor($durationt/3600));
$min = intval(floor($durationt/60))-($hrs*60);
if($hrs<>0){
$rval = $hrs.’ hr’;
}
if($min<>0){
if($hrs<>0){
$rval .= ‘ & ‘.$min.’ min’;
} else {
$rval = $min.’ min’;
}
}
return $rval;
}

How to use it:

$timeOne =’12:31′;

$timeTwo = ’15:43′;

echo duration($timeOne,$timeTwo); //output: 3 hours & 12 min

Advertisements

,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: