homepage Welcome to WebmasterWorld Guest from 54.226.93.128
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Calculating difference between two times
joe in nantucket




msg:3606669
 7:50 pm on Mar 20, 2008 (gmt 0)

Hi everyone,
I started this thread [webmasterworld.com] and after reading the suggestions, decided that storing the current date and time for each page viewed while the session was stored was the way to go.

My problem is now that I want to tell the difference between the two times, the last stored time and the current time, and if it's greater than 20 minutes, to show the user as logged out.

Here are the problems I run into, if the difference is less than 20 minutes but the difference between the two days is more than 1, I don't want to show the user as logged in, obviously.

I have the times currently stored in the strtotime() fashion.

Here's an example:

$lasttime = strtotime("-67 minutes"); // for instance
$currentdate = strtotime("now");
$lastact = date("Y-m-d g:i:s a", $lasttime);
$now = date("Y-m-d g:i:s a", $currentdate);
$daycheck = date("d", $now) - date("d", $lastact);
$minutecheck = date("i", $now) - date("i", $lastact);

this doesn't work because it's not taking the current day, it's taking the total number of days in the month.

If anyone can help me out, I would appreciate it.

 

wtkad




msg:3606688
 8:11 pm on Mar 20, 2008 (gmt 0)

Instead of converting them to dates, try comparing the two times.

$difference = $currentdate - $lasttime;

That gives you how long it's been in seconds. 20 minutes (60 seconds X 20) is 1200 seconds, so if the difference is greater then 1200, it's expired.

joe in nantucket




msg:3607083
 5:43 am on Mar 21, 2008 (gmt 0)

wtkad, I can't believe it's that simple, really. I searched and searched but came across some of the weirdest code and it wouldn't work. Thanks a lot for everything.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved