Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts

    Issue in getting the time difference between two variables

    I have two textboxes that inputted time, and I want to get the difference between two time.

    for example:

    $IN = 13:35;
    $OUT = 17:35;

    $OTHours = ($OUT - $IN);

    $OTHours = 4.00;


    and it is correct, but I have a scenario like this:

    $IN = 21:35;
    $OUT = 05:35;

    $OTHours = -16.00;

    it should be 8.00.

    Any help is highly appreciated.

    Thank you..

  • #2
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    convert the time to timestamp which gives you a seconds value, then subtract the latest from the earliest and you have the time difference in seconds...

    multiply the answer by 60 for minutes
    60 * 60 for hours etc...

    you will need full date and time

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    What if i add date in time and i used strtotime is it much ok?how?


    Thank you

  • #4
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    yep..

    PHP Code:
    $thisTime1 date("U",strtotime($somedate_from_myform));
    $thisTime2 date("U",strtotime($somedate_from_myform2));
    $timeDiff $thisTime2 $thisTime1

  • #5
    Regular Coder
    Join Date
    Jan 2012
    Posts
    134
    Thanks
    0
    Thanked 32 Times in 32 Posts
    Quote Originally Posted by jmj001 View Post
    yep..

    PHP Code:
    $thisTime1 date("U",strtotime($somedate_from_myform));
    $thisTime2 date("U",strtotime($somedate_from_myform2));
    $timeDiff $thisTime2 $thisTime1
    date("U") is redundant when used like that. All you need is:

    PHP Code:
    $in_timestamp strtotime($IN); 
    $out_timestamp strtotime($OUT); 
    $difference $out_timestamp $in_timestamp
    That gives you $difference in seconds, so you can convert that to hours and minutes if you want.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    An alternative on 5.3+ is to use datetime and dateinterval. Dateinterval is always the difference, and sign is not included unless explicitly specified:
    PHP Code:
    $dt1 = new DateTime('13:35');
    $dt2 = new DateTime('17:17');

    $dtdiff $dt1->diff($dt2);
    print 
    $dtdiff->format('%h hours, %i minutes'); 
    That will print '3 hours, 42 minutes'.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •