All In One Script



PHP,HTLM,CSS,Jquery,AJAX,Javascript and etc doubts and sample codes

  • Home
  • Javascript
  • PHP
  • CSS
  • SQL/MYSQL

Get time difference only from hours and minutes

by Blogger 1:58:00 AM hours minutes PHP Time Time Difference

Get time difference only from hours and minutes



 Problem:

I have two times one is $InTime : 12:55:10 and the other is $OutTime : 12:56:10
So I think calculating the time difference should be:
$TotalTimeInOffice = $OutTime - $InTime
I want in hours so I think this should be the result 00:01, but I am getting wrong result. With this calculation I am get 0 in the result, but why?
Here are some examples from database:
InTime is: 12:55:10
OutTime is: 12:56:09
Result Is: 0
InTime is: 12:24:45
OutTime is: 00:00:00
Result Is: -12
InTime is: 10:05:48
OutTime is: 10:06:11
Result Is: 0
and the PHP Code would be.
$timestart = $time_sheets->in_time;
$timestop = $time_sheets->out_time;
$time_diff = $timestop - $timestart ;
    echo "InTime is: $timestart <br />";
    echo "OutTime is: $timestop <br />";
    print_r('Result Is: '.$time_diff."<br />");





Answer :

this code fails when midnight is involved e.g. 12:24:45 - 00:00:00 returns -12:24:45. You need tocorrect the times if they are located on either side of midnight:

function getDuration($timestart, $timestop) {
    $time1 = new DateTime("today " . $timestart);
    $time2 = new DateTime("today " . $timestop);
    if ($time2 < $time1) {
        $time2 = new DateTime("tomorrow " . $timestop);
    }
    $time_diff = $time1->diff($time2);
    echo " InTime is: $timestart\n";
    echo "OutTime is: $timestop\n";
    echo " Result is: " . $time_diff->format("%r%H:%I:%S") . "\n\n";
}
getDuration("12:55:10", "12:56:09");
getDuration("12:24:45", "00:00:00");
getDuration("10:05:48", "10:06:11");
getDuration("11:00:00", "01:00:00");
Output:
 InTime is: 12:55:10
OutTime is: 12:56:09
 Result is: 00:00:59

 InTime is: 12:24:45
OutTime is: 00:00:00
 Result is: 11:35:15

 InTime is: 10:05:48
OutTime is: 10:06:11
 Result is: 00:00:23

 InTime is: 11:00:00
OutTime is: 01:00:00
 Result is: 14:00:00








READ MORE
SHARE :

Search This Blog

Followers

  • Popular
  • Recent
  • Comments
    Serving XHTML and self-closing tags
    Get first key in a (possibly) associative array?
    In Java, difference between default, public, protected, and private
    How to efficiently iterate over each Entry in a Map?
    How to get Real IP,ISP,Country,City and etc from Visitor using PHP
    Solved : curl_init() function not working in Ubuntu
    How can I make the cursor a hand when a user hovers over a list item?
    Does finally always execute in Java?
    How to avoid Java code in JSP files?
    Length of a JavaScript object

Instagram

About

Popular Posts

  • Serving XHTML and self-closing tags
    Serving XHTML and self-closing tags I am trying to follow the xhtml 1.0 strict standard as I am creating my website. Right now, validat...
  • Get first key in a (possibly) associative array?
    Get first key in a (possibly) associative array? What's the best way to determine the first key in a possibly associative array? My...
  • In Java, difference between default, public, protected, and private
    In Java, difference between default, public, protected, and private In Java , are there clear rules on when to use each of access modifi...
  • How to efficiently iterate over each Entry in a Map?
    How to efficiently iterate over each Entry in a Map? If I have an object implementing the  Map  interface in Java and I wish to iterate...
  • How to get Real IP,ISP,Country,City and etc from Visitor using PHP
    How to get Real IP,ISP,Country,City and etc from Visitor using PHP Php Get Real visiter's IP and ISP and Country and City and Countr...
  • Solved : curl_init() function not working in Ubuntu
    Solved : curl_init() function not working in Ubuntu  Here solved the error  Fatal error: Call to undefined function curl_init() ...
  • How can I make the cursor a hand when a user hovers over a list item?
    How can I make the cursor a hand when a user hovers over a list item? I've got a list, and I have a click handler for its items: ...
  • Does finally always execute in Java?
    Does finally always execute in Java? I have a try/catch block with  return s inside it. Will the finally block be called? For example...
  • How to avoid Java code in JSP files?
    How to avoid Java code in JSP files? I'm new to Java EE and I know that something like the following three lines <%= x + 1 %...
  • Length of a JavaScript object
    Length of a JavaScript object If I have a JavaScript object, say var myObject = new Object (); myObject [ "firstname" ] ...

statcounter



statcounter



Template Created By ThemeXpose & Blogger Templates