finding out difference between two times












2














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?










share|improve this question
























  • 143000 is refer to 14:30?? what it means>
    – diEcho
    Mar 3 '11 at 6:21










  • also see stackoverflow.com/questions/676824/…
    – Quamis
    Mar 3 '11 at 7:17










  • echo (new DateTime($time2))->diff(new DateTime($time1))->format('h');
    – Ja͢ck
    May 25 '15 at 3:06
















2














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?










share|improve this question
























  • 143000 is refer to 14:30?? what it means>
    – diEcho
    Mar 3 '11 at 6:21










  • also see stackoverflow.com/questions/676824/…
    – Quamis
    Mar 3 '11 at 7:17










  • echo (new DateTime($time2))->diff(new DateTime($time1))->format('h');
    – Ja͢ck
    May 25 '15 at 3:06














2












2








2


1





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?










share|improve this question















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?







php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 '11 at 15:12









Farray

6,16322736




6,16322736










asked Mar 3 '11 at 6:13









Meera

593410




593410












  • 143000 is refer to 14:30?? what it means>
    – diEcho
    Mar 3 '11 at 6:21










  • also see stackoverflow.com/questions/676824/…
    – Quamis
    Mar 3 '11 at 7:17










  • echo (new DateTime($time2))->diff(new DateTime($time1))->format('h');
    – Ja͢ck
    May 25 '15 at 3:06


















  • 143000 is refer to 14:30?? what it means>
    – diEcho
    Mar 3 '11 at 6:21










  • also see stackoverflow.com/questions/676824/…
    – Quamis
    Mar 3 '11 at 7:17










  • echo (new DateTime($time2))->diff(new DateTime($time1))->format('h');
    – Ja͢ck
    May 25 '15 at 3:06
















143000 is refer to 14:30?? what it means>
– diEcho
Mar 3 '11 at 6:21




143000 is refer to 14:30?? what it means>
– diEcho
Mar 3 '11 at 6:21












also see stackoverflow.com/questions/676824/…
– Quamis
Mar 3 '11 at 7:17




also see stackoverflow.com/questions/676824/…
– Quamis
Mar 3 '11 at 7:17












echo (new DateTime($time2))->diff(new DateTime($time1))->format('h');
– Ja͢ck
May 25 '15 at 3:06




echo (new DateTime($time2))->diff(new DateTime($time1))->format('h');
– Ja͢ck
May 25 '15 at 3:06












7 Answers
7






active

oldest

votes


















3














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





share|improve this answer























  • for some tasks i got the right duration but for some not :(
    – Meera
    Mar 6 '11 at 6:37



















7














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.






share|improve this answer































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





    share|improve this answer































      1














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





      share|improve this answer





















      • it is work fine .thanks
        – pedram shabani
        Nov 3 '18 at 6:15



















      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"






      share|improve this answer





























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






        share|improve this answer























        • thanks dear but do you have any idea my the duration is not correct
          – Meera
          Mar 3 '11 at 6: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 3 '11 at 9:09



















        0














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



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






        share|improve this answer





















        • 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 3 '11 at 9:12











        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5177334%2ffinding-out-difference-between-two-times%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        7 Answers
        7






        active

        oldest

        votes








        7 Answers
        7






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














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





        share|improve this answer























        • for some tasks i got the right duration but for some not :(
          – Meera
          Mar 6 '11 at 6:37
















        3














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





        share|improve this answer























        • for some tasks i got the right duration but for some not :(
          – Meera
          Mar 6 '11 at 6:37














        3












        3








        3






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





        share|improve this answer














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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 5:36









        Tousif Ahmed

        450513




        450513










        answered Mar 3 '11 at 6:27









        diEcho

        37.2k26125197




        37.2k26125197












        • for some tasks i got the right duration but for some not :(
          – Meera
          Mar 6 '11 at 6:37


















        • for some tasks i got the right duration but for some not :(
          – Meera
          Mar 6 '11 at 6:37
















        for some tasks i got the right duration but for some not :(
        – Meera
        Mar 6 '11 at 6:37




        for some tasks i got the right duration but for some not :(
        – Meera
        Mar 6 '11 at 6:37













        7














        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.






        share|improve this answer




























          7














          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.






          share|improve this answer


























            7












            7








            7






            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.






            share|improve this answer














            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 6 '11 at 1:34

























            answered Mar 3 '11 at 6:23









            Farray

            6,16322736




            6,16322736























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





                share|improve this answer




























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





                  share|improve this answer


























                    5












                    5








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





                    share|improve this answer














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






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 15 '12 at 11:37









                    Adi Lester

                    19.7k107194




                    19.7k107194










                    answered Sep 15 '12 at 5:20









                    aravind3

                    232310




                    232310























                        1














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





                        share|improve this answer





















                        • it is work fine .thanks
                          – pedram shabani
                          Nov 3 '18 at 6:15
















                        1














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





                        share|improve this answer





















                        • it is work fine .thanks
                          – pedram shabani
                          Nov 3 '18 at 6:15














                        1












                        1








                        1






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





                        share|improve this answer












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






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Oct 30 '16 at 18:25









                        WhiteOne

                        3621617




                        3621617












                        • it is work fine .thanks
                          – pedram shabani
                          Nov 3 '18 at 6:15


















                        • it is work fine .thanks
                          – pedram shabani
                          Nov 3 '18 at 6:15
















                        it is work fine .thanks
                        – pedram shabani
                        Nov 3 '18 at 6:15




                        it is work fine .thanks
                        – pedram shabani
                        Nov 3 '18 at 6:15











                        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"






                        share|improve this answer


























                          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"






                          share|improve this answer
























                            0












                            0








                            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"






                            share|improve this answer













                            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"







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 3 '11 at 6:25









                            itsraja

                            71431740




                            71431740























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






                                share|improve this answer























                                • thanks dear but do you have any idea my the duration is not correct
                                  – Meera
                                  Mar 3 '11 at 6: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 3 '11 at 9:09
















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






                                share|improve this answer























                                • thanks dear but do you have any idea my the duration is not correct
                                  – Meera
                                  Mar 3 '11 at 6: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 3 '11 at 9:09














                                0












                                0








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






                                share|improve this answer














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







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Mar 3 '11 at 6:28

























                                answered Mar 3 '11 at 6:15









                                Long Ears

                                4,3061515




                                4,3061515












                                • thanks dear but do you have any idea my the duration is not correct
                                  – Meera
                                  Mar 3 '11 at 6: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 3 '11 at 9:09


















                                • thanks dear but do you have any idea my the duration is not correct
                                  – Meera
                                  Mar 3 '11 at 6: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 3 '11 at 9:09
















                                thanks dear but do you have any idea my the duration is not correct
                                – Meera
                                Mar 3 '11 at 6:21




                                thanks dear but do you have any idea my the duration is not correct
                                – Meera
                                Mar 3 '11 at 6: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 3 '11 at 9:09




                                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 3 '11 at 9:09











                                0














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



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






                                share|improve this answer





















                                • 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 3 '11 at 9:12
















                                0














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



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






                                share|improve this answer





















                                • 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 3 '11 at 9:12














                                0












                                0








                                0






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



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






                                share|improve this answer












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



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







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Mar 3 '11 at 6:36









                                sadeesh kumar

                                3251411




                                3251411












                                • 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 3 '11 at 9:12


















                                • 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 3 '11 at 9:12
















                                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 3 '11 at 9:12




                                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 3 '11 at 9:12


















                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5177334%2ffinding-out-difference-between-two-times%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Sphinx de Gizeh

                                Dijon

                                Guerrita