COUNTDOWN

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

COUNTDOWN

Post 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 :?:
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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(). ;)
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

That is the problem, I don't know where to save the time, I am using mysql for the site.
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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(&quote;insert into myTable (time) values (now())&quote;)
  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.
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post 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.
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post 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.
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post 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 :)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post 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))&quote;;
If I wanted for example the value 'jail' to be time I just do this:

Code: Select all

jail time DEFAULT '0' NOT NULL
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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...
Post Reply