Jump to content

[SOLVED] Difference between 2 time strings


johnadamson

Recommended Posts

HELP!!

 

I am trying to calculate the difference between 2 time strings. e.g:

 

$time1 = "14:00:03";
$time2 = "12:30:03";

 

The difference needs to be in hours:minutes:seconds. The 2 times will always be different as they are taken from a database (the 2 times above are just examples of how the time is presented).

 

I would VERY much appreciate any help.

 

Thank you in advance.

Link to comment
https://forums.phpfreaks.com/topic/51634-solved-difference-between-2-time-strings/
Share on other sites

The code has been working FANTASTIC, but for some reason I am seeing the following:

 

$time1 = "12:55:00";
$time2 = "12:55:32";
echo date("H:i:s",(strtotime($time2) - strtotime($time1)));

 

When I run this it comes back saying that the time difference is 01:00:32, when it should be 00:00:32 (32 seconds).

 

Any ideas would be very much appreciated.

I have just tried it again and get the same. I tried using different value:

 

$time1 = "06:55:00";
$time2 = "08:55:32";
echo date("H:i:s",(strtotime($time2) - strtotime($time1)));

 

and I get 03:00:32 when it should be 02:00:32. I really have got no idea why it is doing it.

If you are connecting to database then better to make diff by query like this

 

SELECT TIMEDIFF('12:55:32', '12:55:00')

 

or

 


<?php

/**
* Function to calculate date or time difference.
* 
* Function to calculate date or time difference. Returns an array or
* false on error.
*
* @author       J de Silva                             <[email protected]>
* @copyright    Copyright © 2005, J de Silva
* @link         http://www.gidnetwork.com/b-16.html    Get the date / time difference with PHP
* @param        string                                 $start
* @param        string                                 $end
* @return       array
*/
function get_time_difference( $start, $end )
{
    $uts['start']      =    strtotime( $start );
    $uts['end']        =    strtotime( $end );
    if( $uts['start']!==-1 && $uts['end']!==-1 )
    {
        if( $uts['end'] >= $uts['start'] )
        {
            $diff    =    $uts['end'] - $uts['start'];
            if( $days=intval((floor($diff/86400))) )
                $diff = $diff % 86400;
            if( $hours=intval((floor($diff/3600))) )
                $diff = $diff % 3600;
            if( $minutes=intval((floor($diff/60))) )
                $diff = $diff % 60;
            $diff    =    intval( $diff );            
            return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) );
        }
        else
        {
            trigger_error( "Ending date/time is earlier than the start date/time", E_USER_WARNING );
        }
    }
    else
    {
        trigger_error( "Invalid date/time data detected", E_USER_WARNING );
    }
    return( false );
}
// a START time value
$start = '12:55:00';
// an END time value
$end   = '12:55:32';

// what is the time difference between $end and $start?
if( $diff=@get_time_difference($start, $end) )
{
  echo "Hours: " .
       sprintf( '%02d:%02d:%02d', $diff['hours'], $diff['minutes'],$diff['seconds'] );
}
else
{
  echo "Hours: Error";
}


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.