View Full Version : Working with Time

12-07-2003, 11:07 PM
Hello all another question for you hehe,

Now,, This is what I am looking to do...

1. A user logs in, a time is entered into his/her database.

2. When they come back and try to login again, the database is checked for there last login, if it has been 24 hours since the last login they get a point, if not they are just logged in and can move about the site...

Now I did this in perl by useing the mktime() module...

I have no clue How I can accomplish this in php...

In perl I turned the time to unix and hten subtracted the two times...

Is there a way to do that in php?

Any help is most apprecaited!

12-08-2003, 12:21 AM
you can basically do it the same way

php has a whole host of date and time functions, but rather annoyingly there's no simple comparison function.

http://uk.php.net/manual/en/ref.datetime.php is a list of all the functions.

The one you'll be interested in is time(); just call that to return a unix timestamp, personally i like to just throw this into an integer field in mysql, but most people here would prolly advise against this.

If you do choose to do it my way then you can store the timestamp in the DB and just use the following for 24 hours ago:

$back24 = time() - 86400;

then just select from the DB where date is less than $back24.

12-08-2003, 12:52 AM
You can do this much easier and efficient through SQL.

You didn't specify your db-format or anythinh, but suppose you hav mySQL and you run an update on your usertable and lastlogin is the tile of your previous login, then your condition would look like :

$sql=("UPDATE usertable SET points = points+1 WHERE (lastlogin + INTERVAL 24 HOUR) > Now() and user=" . $_POST[user] );

Every db has its own goodies for doing stuff like this.

12-08-2003, 02:56 AM
I will check out the time() module thanks readMe...

I am using noraml .dbs as in -- NAMEOFFILE.db

To put mySQL on the server is 50 dollars and my client can't afford it yet so We are going with .db...

Anyways, Im pretty sure that timestamp in time() will serve the purpose very nicly...