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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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                             <giddomains@gmail.com>
* @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";
}


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.