Page 1 of 3
COUNTDOWN
Posted: Mon May 16, 2005 2:27 pm
by Parody
This is probably extremely easy but, how do I create a countdown timer that can be different for each member on a site, and can be started using a php script.
If that makes sense

Posted: Mon May 16, 2005 3:28 pm
by Skara
as in counting days?
whenever they join, save time() somewhere. Then, evertime the page is loaded, check the difference between the current time() and the saved time().

Posted: Mon May 16, 2005 3:31 pm
by Parody
That is the problem, I don't know where to save the time, I am using mysql for the site.
...
Posted: Mon May 16, 2005 4:44 pm
by Calimero
I would use JavaScript for this - if you just user to start the timer, and use it on one page, if needed on several pages - we need info in what purpose would you need it - for what to use it.
Posted: Mon May 16, 2005 5:37 pm
by Burrito
I agree in that JS would give you a "prettier" counter (you could count in realtime), but if it's going to need to be specific to each user, you'd be best suited to use a combination of JS and php.
do as Skara suggested and stamp the time if their original visit (or whatever you want to count FROM).
do that in mysql:
Code: Select all
mysql_query("e;insert into myTable (time) values (now())"e;)
or die(mysql_error());
then fetch that time and send it down to the client to start your JS timer. I'm sure there are a million JS timers out there that you could use for code...google it.
Posted: Tue May 17, 2005 1:15 am
by Parody
I will explain more.
When the user goes on a page I need the countdown timer to start, it does not need to be realtime. The whole idea of the countdown is so that the user cannot visit the same page within a certain amount of time.
Posted: Tue May 17, 2005 2:28 am
by hongco
Parody wrote:The whole idea of the countdown is so that the user cannot visit the same page within a certain amount of time.
2 options:
- use cookie: user can only go back to that page after cookie expired. however, this method is not secure - someone can delete the cookie if he knows how to.
- php/mysql: save time when he first goes to the page, and when he revisited. check the first time record with the current time.
Posted: Thu May 19, 2005 11:24 am
by Parody
Thanks, but I'm still stuck, I went with the MYSQL option, but I can't seem to understand the time idea. Can someone give me an example of the coding for creating a value in a database which is a timestamp and how to recall it?
Thanks

Posted: Thu May 19, 2005 11:35 am
by Burrito
Make sure you have a field in the database that is set up as a time or a datetime then use:
Code: Select all
insert into myTable (myTimeField) values (now())
that will stamp the current time on the server.
then to recall it just select off the table with a normal select statement and you can format the time however you want using php's
date() function.
Posted: Thu May 19, 2005 11:39 am
by Parody
How do I create a time field in a MYSQL database?
I know how to do the normal stuff, just not the time
Posted: Thu May 19, 2005 11:43 am
by Burrito
when you're creating the table, (assuming you're using phpMyAdmin), select "time" or "datetime" from the field type list ie: varchar, char, text, longtext, etc..
if you're doing it from the command line on an already existing table, use something like this:
Code: Select all
ALTER TABLE myTable ADD COLUMN myTimeField time AFTER whateverFieldYouWantThisAfter
Posted: Thu May 19, 2005 11:48 am
by Parody
I'm not using phpmyadmin, I am coding it all by using php to create the tables, its so I can quickly up and go from servers if i need to
Posted: Thu May 19, 2005 11:50 am
by Burrito
ok then try this:
Code: Select all
mysql_query("ALTER TABLE myTable ADD COLUMN myTimeField time AFTER whateverFieldYouWantThisAfter")
or die(mysql_error());
edit: if you want the field at the end...then just leave off the the after portion.
Posted: Thu May 19, 2005 11:56 am
by Parody
So... If I wanted to add it into something like this:
Code: Select all
$create = CREATE TABLE stats (username varchar(40),character int(2) DEFAULT '01' NOT NULL,health int(3) DEFAULT '100' NOT NULL,money int(100) DEFAULT '2000' NOT NULL,rank int(3) DEFAULT '01' NOT NULL,level int(2) DEFAULT '001' NOT NULL,respect int(3) DEFAULT '001' NOT NULL,house int(2) DEFAULT '01',cstat int(3) DEFAULT '001' NOT NULL,gstat int(3) DEFAULT '001' NOT NULL,status int(1) DEFAULT '1' NOT NULL,jail int(1) DEFAULT '0' NOT NULL,PRIMARY KEY (username))"e;;
If I wanted for example the value 'jail' to be time I just do this:
Posted: Thu May 19, 2005 12:01 pm
by Burrito
don't put the default in there.
you might need to add a lenth to it as well, can't remember off the top of my head, hit up the mysql man...