6

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?

Farray
  • 8,290
  • 3
  • 33
  • 37
Meera
  • 149
  • 3
  • 4
  • 10

7 Answers7

10

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.

Farray
  • 8,290
  • 3
  • 33
  • 37
  • echo floor($hours) . ':' . floor( ($hours-floor($hours)) * 60 ); it should be like this else minutes will display in points sometimes – Sunil Dabhi Aug 08 '23 at 10:24
5
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");
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
aravind3
  • 272
  • 3
  • 11
5

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 ';
Captain Sparrow
  • 1,114
  • 17
  • 26
xkeshav
  • 53,360
  • 44
  • 177
  • 245
2

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');
WhiteOne
  • 887
  • 1
  • 10
  • 20
0

You need strtotime('14:30') rather than strtotime(143000)

Edit: Actually to my surprise, strtotime(143000) does seem to have the desired effect but only for double-digit hours so I still wouldn't rely on it. Anyway it's not the cause of your problem ;)

Long Ears
  • 4,886
  • 1
  • 21
  • 16
  • thanks dear but do you have any idea my the duration is not correct – Meera Mar 03 '11 at 06:21
  • ok thanks for all who tried to help well now for some tasks the duration is correct but for some is still wrong like someont start his task at 09:11:00 and finish at 09:33:00 and it gives me 4 hr duration?! – Meera Mar 03 '11 at 09:09
0

for example the start time is 09:19:00 and end time is 11:01:00 but it give me duration time only 1 hour which is wrong

You are calculating the difference in hours. what is the correct result for "start time is 09:19:00 and end time is 11:01:00"

itsraja
  • 1,640
  • 4
  • 32
  • 48
0

You can use $hour = ($end - $t1)/(60*60)

In this the time format is (seconds*minutes*days*months*years) => (60*60*2)

sadeesh kumar
  • 475
  • 1
  • 5
  • 11
  • ok thanks for all who tried to help well now for some tasks the duration is correct but for some is still wrong like someont start his task at 09:11:00 and finish at 09:33:00 and it gives me 4 hr duration?! – Meera Mar 03 '11 at 09:12