Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding out difference between two times

Tags:

php

I wrote the following code to determine the amount of time that employees spend on a task:

$time1 = $row_TicketRS['OpenTime'];
$time2= $row_TicketRS['CloseTime'];

$t1=strtotime($time1); 
$t2=strtotime($time2);


$end=strtotime(143000);  //143000 is reference to 14:30


//$Hours =floor((($t2 - $t1)/60)/60); 

$Hours = floor((($end- $t1)/60)/60);


echo   $Hours.' Hours '; 

The above code is not giving me the correct time.

For example, with a start time of 09:19:00 and end time of 11:01:00 it give me duration time of only 1 hour which is wrong. What is the correct way?

like image 455
Meera Avatar asked Mar 03 '11 06:03

Meera


People also ask

How do you find the difference in time between two times?

Calculate the duration between two times The goal is to subtract the starting time from the ending time under the correct conditions. If the times are not already in 24-hour time, convert them to 24-hour time. AM hours are the same in both 12-hour and 24-hour time.

How do you find the difference between two data?

Calculate the difference between two numbers by inputting a formula in a new, blank cell. If A1 and B1 are both numeric values, you can use the "=A1-B1" formula. Your cells don't have to be in the same order as your formula. For example, you can also use the "=B1-A1" formula to calculate a different value.


4 Answers

Your use of floor is why you are getting only 1 hour for those inputs. Those inputs result in 1.7 hours if you keep the answer as a float. floor automatically rounds down to the lower integer value. Check out http://php.net/manual/en/function.floor.php for more info.

$t1 = strtotime('09:19:00');
$t2 = strtotime('11:01:00');
$hours = ($t2 - $t1)/3600;   //$hours = 1.7

If you want a more fine-grained time difference, you can flesh it out...

echo floor($hours) . ':' . ( ($hours-floor($hours)) * 60 );  // Outputs "1:42"

UPDATE:

I just noted your comments on Long Ears' answer. Please check my comments above again, they are correct. Inputting values of '09:11:00' and '09:33:00' results in 0 hours (22 minutes).

If you input those values and got 4 hours, you likely have a decimal error in your math. Using '09:11' to '09:33', the result is .367 hours. If you divided the strtotime results by 360 instead of by 3600, you would get result 3.67 hours (or 4 hours, depending on your rounding method).

strtotime converts your time to an int value representing number of seconds since Unix epoch. Since you convert both values to seconds, and then subtract the values from each other, the resulting value is a quantity of seconds. There are 3600 seconds in 1 hour.

like image 178
Farray Avatar answered Nov 14 '22 22:11

Farray


After changing strtotime('14:30:00') everything working fine.. see below

$time1 = '09:19:00';
$time2= '11:01:00';

echo "Time1:".$t1=strtotime($time1); 
echo "<br/>Time2:".$t2=strtotime($time2);    

echo "<br/>End:".$end=strtotime('14:30:00'); 
echo  "<br/>Floor value:";  
var_dump(floor((($end- $t1)/60)/60));     

//$Hours =floor((($t2 - $t1)/60)/60); 

$Hours = floor((($end- $t1)/60)/60);    

echo   $Hours.' Hours ';
like image 45
diEcho Avatar answered Nov 14 '22 22:11

diEcho


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')));

    //Hour

    $hours=floor($diff/(60*60));

    //Minute 

    $mins=floor(($diff-($hours*60*60))/(60));

    //Second

    $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;

}

echo getTimeDiff("23:30","01:30");
like image 38
aravind3 Avatar answered Nov 15 '22 00:11

aravind3


A better way is to use http://php.net/manual/en/datetime.diff.php

$start_t = new DateTime($start_time);
$current_t = new DateTime($current_time);
$difference = $start_t ->diff($current_t );
$return_time = $difference ->format('%H:%I:%S');
like image 24
WhiteOne Avatar answered Nov 15 '22 00:11

WhiteOne