time difference from time stamp

Discussion in 'PHP' started by assgar, Sep 6, 2008.

  1. #1
    Hi

    I am trying to get the number of hours and minutes between
    two time time stamps.

    The expected results should be 1:59 (h:mm) instead I am getting 1:58 (h:mm).
    What is wrong with my approach?

    Is it also possible to use a built in php function so I don't have to
    divide by 60 to convert to hours and minutes?

    [code]
    <?
    
    $end_time = "2008-09-05 20:59:13";
    $start_time = "2008-09-05 19:00:16";
    
    $end = date("h:i:s",strtotime($end_time));
    $start = date("h:i:s",strtotime($start_time));
    $diff = strtotime($end)  - strtotime($start);
      
    //convert to min and sec
    $convert_min = $diff/60;
    $convert_sec = $diff % 60;//seconds
    
    //convert to hours and min
    $convert_hr = floor($convert_min/60);//hours
    $remainder = floor($convert_min % 60);//minutes
    	
    $total_visit = 	$convert_hr.":".$remainder;	
    
    ?>
    
    PHP:
    [/code]
     
    assgar, Sep 6, 2008 IP
  2. Shoro

    Shoro Peon

    Messages:
    143
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The difference between these times isn't 1:59. It's 1:58.95, and the floor command on the remainder line results in it being rounded down to 1:58. If you want it to round up, replace the floor on that line with ceil. If you want it to round off, replace it with round.
     
    Shoro, Sep 6, 2008 IP