# Thread: The difference between times

1. ## The difference between times

Ok I am about ready to pull my hair out. I am trying to get the total time in hours/minutes that a person is on my site.

I have a table that stores the date and a timestamp that a user logged into my web site and a timestamp that someone logged off. The input I am using for both timestamps is:

// on the log out page
\$logout = date("Y-m-d\TH:i:s");

when I try to subtract these I get 0 every time. Since most of my users will be on for no more than 2 hours how can I record the total time on the web site?

Thanks,

Ok I am about ready to pull my hair out. I am trying to get the total time in hours/minutes that a person is on my site.

I have a table that stores the date and a timestamp that a user logged into my web site and a timestamp that someone logged off. The input I am using for both timestamps is:

// on the log out page
\$logout = date("Y-m-d\TH:i:s" );

when I try to subtract these I get 0 every time. Since most of my users will be on for no more than 2 hours how can I record the total time on the web site?

Thanks,

Code:
```if (!function_exists('get_microtime')){
function get_microtime(){
\$mtime=microtime();
\$mtime=explode(" ",\$mtime);
\$mtime=doubleval(\$mtime[1])+doubleval(\$mtime[0]);
return \$mtime;
}
}
\$time1=get_microtime();
\$time2=get_microtime();

//difference is in seconds
\$diff=\$time1-\$time2;

```
Cheers,
Keith.

// on the log out page
\$logout = date("Y-m-d\TH:i:s");
I think the problem you're having stems from the fact that you're trying to use two strings in mathematical context, as numbers. The date() function returns a string. If you need a two numbers that you can subtract, you can use the code from Keith's example, or, if you don't care about sub-second precision, just use the time() function.

I thought PHP had a function to format a number of seconds into h:m:s, but I can't find it. It wouldn't be difficult to make one, though.

4. This function returns the difference of two times in seconds:

PHP Code:
``` function timeDifference(\$t1, \$t2){    return strtotime(\$t1) - strtotime(\$t2);}  ```

5. Originally Posted by Robo
This function returns the difference of two times in seconds...
I think this would be a good idea if one started out with times in string representation, but for skadera's problem, it makes more sense to start out with time() and just convert the final result to a string, rather than getting integers, converting them to strings (which is what the date() function would do), then converting them back to integers, then subtracting them, then converting the final result back to a string.

6. Thanks to all who posted. I have used Keith's script with time() instead of microtime(). So far it is working fine. Now all I need to do is get the total hours and minutes for the previous sesson, current month, and total. I have some code to work with on this but I may post something else later if I need some more help. Thanks

Scott

#### Posting Permissions

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