1. Registered User
Join Date
Jan 2012
Location
pune
Posts
16

Hello Friends..

I want to calculate time difference (in H:i:s format )between two times in php...i tried like this

\$t1= 08:32:46;
\$t2=08:34:56;

\$t1=strtotime(08:32:46);
\$t2=strtotime(08:34:56);

\$t3 =date("H:i:s",\$t2-\$t1);
I got answer something like this ... 05:31:57

and after that i tried using date with time like this

\$t1= 2012 - 02-08 08:32:46;
\$t2=2012 - 02-08 08:34:56;

\$t3 =date("H:i:s",\$t2-\$t1);
I got answer something like this ...1970-01-01 05:31:57 ...in unixtimestamp its giving i think..

Pleaase tel me how to calculate timings .

2. Registered User
Join Date
Apr 2005
Posts
29
I would use the 'Mysql Time Difference Function'. For example using TimeDiff('Year1 Time1', 'Year2 Time2') This query method returns the time difference between any two consecutive dates.

TimeDiff('Year1 Time1', 'Year2 Time2')
Code:
```mysql> SELECT TIMEDIFF('2009:01:08 00:00:00', '2008:01:01 00:00:00.000001');
+---------------------------------------------------------------+
| TIMEDIFF('2009:01:08 00:00:00', '2008:01:01 00:00:00.000001') |
+---------------------------------------------------------------+
| 8951:59:59.999999 |
+---------------------------------------------------------------+
1 row in set (0.00 sec)```

3. Programming since 1BC
Join Date
Sep 2009
Location
Ontario
Posts
1,057
Or do it the long way.
Convert hh:mm:ss to seconds, subtract start time from end time, and convert back to hh:mm:ss
Code:
```diff=(end_hh*3600+end_mm*60+end_ss) - (start_hh *3600 + start_mm*60+start_ss)
diff_hh=int(diff/3600)
diff_mm=int(mod(diff,3600)/60)
diff_ss=mod(diff,60)```
Assuming everything happens on the same day, and you are using a 24 hour clock.

Join Date
Nov 2004
Location
out on a limb
Posts
13,692
if you are wanting to do this inside PHP
..and assuming that you have to valid time values then use the PHP date time functions
PHP: Date/Time Functions - Manual

such as datediff

which returns an interval object from which you can derive the elapsed whatever (years,months, hours, minutes, seconds whatever)

if you cannot constrain your values then explcitly convert them to dates using strtotime

#### Posting Permissions

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