PDA

View Full Version : Problem in getting time difference

Nirbhay
08-28-2009, 07:44 AM
Hi All,

I need to find the time difference in the format like :

\$First_time = '18:20:25';
\$Second_time = '12:00:00';
\$diff = '6:20:25';

can anyone help me in getting such result.

Thanx in advance ....:)

mlseim
08-28-2009, 01:55 PM
I found the script (below) using Google, but didn't test it:

function getMytimeDiff(\$t1,\$t2)
{
\$a1 = explode(":",\$t1);
\$a2 = explode(":",\$t2);
\$time1 = ((\$a1[0]*60*60)+(\$a1[1]*60)+(\$a1[2]));
\$time2 = ((\$a2[0]*60*60)+(\$a2[1]*60)+(\$a2[2]));
\$diff = abs(\$time1-\$time2);
\$hours = floor(\$diff/(60*60));
\$mins = floor((\$diff-(\$hours*60*60))/(60));
\$secs = floor((\$diff-((\$hours*60*60)+(\$mins*60))));
\$result = \$hours.":".\$mins.":".\$secs;
return \$result;
}
\$mytime1 = "14:05:08";
\$mytime2 = "03:22:54";
\$cool = getMytimeDiff(\$mytime1,\$mytime2);

bacterozoid
08-28-2009, 02:30 PM
I tested that function and it does indeed work. I also tried to find the difference like this:

\$time1 = '18:20:25';
\$time2 = '12:00:00';
\$diff = strtotime(\$time1) - strtotime(\$time2);

I thought I would have an edge, but the already provided function is about 5 times faster using the math and exploding.