Time in hours minus another time

Discussion in 'PHP' started by zodiakasxxx, Jul 29, 2009.

  1. #1
    For example, I have first time 11:32:48 and second is 5:12:53 .
    Format is Hours:minutes:seconds
    I need to do 11:32:48-5:12:53=xx:xx:xx with PHP. Can anyone help me? Do I need to convert it to seconds?
     
    zodiakasxxx, Jul 29, 2009 IP
  2. tguillea

    tguillea Active Member

    Messages:
    229
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #2
    easy if you can use explode.

    
    $time1 = 11:32:48;
    $time2 = 5:12:53;
    $time1 = explode(':',$time1);
    $time2 = explode(':',$time2);
    $result[0] = $time1[0] - $time2[0];
    $result[1] = $time1[1] - $time2[1];
    $result[2] = $time1[2] - $time2[2];
    $difference = $result[0].':'.$result[1].':'.$result[2];
    
    PHP:
    Obviously you can simplify this quite a bit but you should get the idea. $difference will be the result.

    Only thing to take note of, make sure you can differentiate between AM/PM. To most easily do this, make sure that the hours are in 24-hour mode and not 12-hour mode. Hope that helps!
     
    tguillea, Jul 29, 2009 IP