Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Problem in getting time difference

1. ## Problem in getting time difference

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.

2. I found the script (below) using Google, but didn't test it:

PHP Code:
``` 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);  ```

3. I tested that function and it does indeed work. I also tried to find the difference like this:

PHP Code:
``` \$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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•