Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-28-2009, 06:44 AM   PM User | #1
Nirbhay
New Coder

 
Join Date: May 2009
Location: Bangalore
Posts: 75
Thanks: 5
Thanked 0 Times in 0 Posts
Nirbhay is an unknown quantity at this point
Question 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.

Thanx in advance ....
Nirbhay is offline   Reply With Quote
Old 08-28-2009, 12:55 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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); 
mlseim is offline   Reply With Quote
Old 08-28-2009, 01:30 PM   PM User | #3
bacterozoid
Regular Coder

 
bacterozoid's Avatar
 
Join Date: Jun 2002
Location: USA
Posts: 489
Thanks: 24
Thanked 35 Times in 35 Posts
bacterozoid is an unknown quantity at this point
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.
bacterozoid is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:08 AM.


Advertisement
Log in to turn off these ads.