finding out difference between two times

前端 未结 7 1962
醉话见心
醉话见心 2021-01-14 04:03

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

$time1 = $row_TicketRS[\'OpenTime\'];
$time2= $row_TicketRS[\'Clos         


        
相关标签:
7条回答
  • 2021-01-14 04:21

    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 ';
    
    0 讨论(0)
  • 2021-01-14 04:25

    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');
    
    0 讨论(0)
  • 2021-01-14 04:25

    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"

    0 讨论(0)
  • 2021-01-14 04:33

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

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

    0 讨论(0)
  • 2021-01-14 04:36

    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.

    0 讨论(0)
  • 2021-01-14 04:38
    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");
    
    0 讨论(0)
提交回复
热议问题