Jump to content

PHP time difference


mattichu

Recommended Posts

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 ?

Link to comment
https://forums.phpfreaks.com/topic/257926-php-time-difference/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/257926-php-time-difference/#findComment-1322048
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257926-php-time-difference/#findComment-1322069
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.