Welcome to WebmasterWorld Guest from 54.196.214.35

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

# Calculating difference between two times

#### joe in nantucket

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

#### New User

joined:Jan 30, 2008
posts: 26

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.

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

#### Junior Member

joined:May 17, 2006
posts:48

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

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

#### New User

joined:Jan 30, 2008
posts: 26

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.