If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > PHP DateTime Calculation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-16-12, 02:03
kirankumer kirankumer is offline
Registered User
 
Join Date: Jan 2012
Location: pune
Posts: 16
Arrow PHP DateTime Calculation

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 .
Reply With Quote
  #2 (permalink)  
Old 02-20-12, 16:46
sonic10 sonic10 is offline
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)
__________________
Web Database Software - Build your own php mysql database.
Reply With Quote
  #3 (permalink)  
Old 02-20-12, 18:12
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
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.
Reply With Quote
  #4 (permalink)  
Old 02-21-12, 03:09
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
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
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On