SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    durango
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 in page
    $login = date("Y-m-d\TH:i:s");

    // 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,


  2. #2
    SitePoint Addict
    Join Date
    Aug 2002
    Location
    Ottawa, Ontario, Canada
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by skadera
    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 in page
    $login = date("Y-m-d\TH:i:s" );

    // 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.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by skadera
    // on the log in page
    $login = date("Y-m-d\TH:i:s");

    // 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.
    Last edited by ThreeDee; Mar 22, 2003 at 13:42.

  4. #4
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This function returns the difference of two times in seconds:

    PHP Code:
    function timeDifference($t1$t2)
    {
        return 
    strtotime($t1) - strtotime($t2);

    Work smarter, not harder. -Scrooge McDuck

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote 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. #6
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    durango
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

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