mattichu Posted February 28, 2012 Share Posted February 28, 2012 hiii, Im wanting to calculate the time difference between $out2 and $in2 and out put it into a spreadsheet: $m=0; while ($m < $num9) { $fullname=mysql_result($result9,$m,"username"); $date=mysql_result($result9,$m,"date"); $in1=mysql_result($result9,$m,"in1"); $out1=mysql_result($result9,$m,"out1"); $in2=mysql_result($result9,$m,"in2"); $out2=mysql_result($result9,$m,"out2"); $out2f=date("G:i:s",strtotime($out2)); $in2f=date("G:i:s",strtotime($in2)); echo date("D",strtotime($date)). "\t" . $fullname . "\t" . $in1 . "\t". $out1 . "\t" . $in2 . "\t" . $out2 . "\t" . date("G:i:s",strtotime($out2f-$in2f)) . "\t" . "\n"; $m++; } header("Content-disposition: attachment; filename=spreadsheet.xls"); why does this code return 00:00:00 ? Quote Link to comment https://forums.phpfreaks.com/topic/257926-php-time-difference/ Share on other sites More sharing options...
Pikachu2000 Posted February 28, 2012 Share Posted February 28, 2012 What are the actual values of $out2 and $in2? Quote Link to comment https://forums.phpfreaks.com/topic/257926-php-time-difference/#findComment-1322039 Share on other sites More sharing options...
mattichu Posted February 28, 2012 Author Share Posted February 28, 2012 $out2 = 18:56:36 $in2 = 18:56:05 Quote Link to comment https://forums.phpfreaks.com/topic/257926-php-time-difference/#findComment-1322043 Share on other sites More sharing options...
mattichu Posted February 28, 2012 Author Share Posted February 28, 2012 so the difference should be 00:00:05 Quote Link to comment https://forums.phpfreaks.com/topic/257926-php-time-difference/#findComment-1322044 Share on other sites More sharing options...
Pikachu2000 Posted February 28, 2012 Share Posted February 28, 2012 Those values are ambiguous. If you had 23:59:30 and 00:02:00, it will come out to 23 hours, 57 minutes, 30 seconds even though it's obvious that isn't what you intend. Computers don't have the ability to reason; you have to supply all of the necessary information. Do you have the date value associated with the record in the database somehow? Quote Link to comment https://forums.phpfreaks.com/topic/257926-php-time-difference/#findComment-1322048 Share on other sites More sharing options...
mattichu Posted February 28, 2012 Author Share Posted February 28, 2012 The databse has it stored as a 'TIME' Quote Link to comment https://forums.phpfreaks.com/topic/257926-php-time-difference/#findComment-1322065 Share on other sites More sharing options...
ManiacDan Posted February 28, 2012 Share Posted February 28, 2012 1) It returns zero because you're asking PHP to subtract two formatted strings, both of which will resolve to 18. You need to work on timestamps or numbers, not formatted time strings. 2) You need DATETIME or TIMESTAMP, not just TIME. TIME, as pika pointed out, will be ambiguous unless you can guarantee that the times will always be on the same date, and/or the first one comes before the second one. Regardless, to get a timestamp you need to use a date and a time in strtotime or mktime. 3) You will eventually go insane if you keep naming your variables $result9 and $m2. Name them what they are. Quote Link to comment https://forums.phpfreaks.com/topic/257926-php-time-difference/#findComment-1322069 Share on other sites More sharing options...
mattichu Posted February 28, 2012 Author Share Posted February 28, 2012 Solved - Cheers guys! x Quote Link to comment https://forums.phpfreaks.com/topic/257926-php-time-difference/#findComment-1322070 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.