PHP: Updated
by Chris "DiGi" Timberlake · in General Discussion · 09/21/2005 (11:01 pm) · 7 replies
Is it possible to update a PHP Script, or time every second? I want to create an active timer, is this possible?
(Sorry, donno where else to post about PHP Questions.)
(Sorry, donno where else to post about PHP Questions.)
About the author
#2
Do a search for using MySQL, you could just update a var in a database, and when a user loads up a specific php file on the server, it would do a simple check on the mysql server to see if a certain game is running. In the database you would save the fallowing:
Game Name
Game Start Time
Game End Time (if it was a timed game)
Game ID (games count plus one, used for game managment)
(I am not a SQL expert so I am just shooting in the dark with the fallowing) These would be dynamic information, what I mean by that is the information would only be there if the current data was in the server. You could use IDs for each game, this way you can keep track of multiple games via an array, and be able to dynamically add and take away games as they are started/ended.
What the PHP script would do is get the gamecount var from the server, and go through the list of games. Say we are displaying game[1]. we would display the gametime as this: $gameTime = $currentTime - $gameStartTime. currentTime would be a php system variable (not sure of the actual php command to get the current time). You would have to do a time convertion to have the game servers time zone match the website servers timezone. Not to mention you would have to do some sort of sort of the game server array (php probably has built in functionality for this).
Sorry to anyone who looks at my explanation and says (this guy doesn't know mySQL or php...?). I just know a few bare minimum basics of both, never dove to deeply into that area of programming.
try checking out MySQL++ (a c++ lib for MySQL). And like I said, do a search in GG for MySQL, I have seen a couple resources around that tackle dealing with MySQL.
09/22/2005 (12:40 am)
Well, say you where wanting to start a timer so you can see dynamically how long a current multiplayer game has been played, there would be a few ways to do this. (These are just my speculations, they may not be THE best way to do it).Do a search for using MySQL, you could just update a var in a database, and when a user loads up a specific php file on the server, it would do a simple check on the mysql server to see if a certain game is running. In the database you would save the fallowing:
Game Name
Game Start Time
Game End Time (if it was a timed game)
Game ID (games count plus one, used for game managment)
(I am not a SQL expert so I am just shooting in the dark with the fallowing) These would be dynamic information, what I mean by that is the information would only be there if the current data was in the server. You could use IDs for each game, this way you can keep track of multiple games via an array, and be able to dynamically add and take away games as they are started/ended.
What the PHP script would do is get the gamecount var from the server, and go through the list of games. Say we are displaying game[1]. we would display the gametime as this: $gameTime = $currentTime - $gameStartTime. currentTime would be a php system variable (not sure of the actual php command to get the current time). You would have to do a time convertion to have the game servers time zone match the website servers timezone. Not to mention you would have to do some sort of sort of the game server array (php probably has built in functionality for this).
Sorry to anyone who looks at my explanation and says (this guy doesn't know mySQL or php...?). I just know a few bare minimum basics of both, never dove to deeply into that area of programming.
try checking out MySQL++ (a c++ lib for MySQL). And like I said, do a search in GG for MySQL, I have seen a couple resources around that tackle dealing with MySQL.
#3
(Sorry about the first post, it was late @ night.)
09/22/2005 (8:35 am)
It wouldn't be for a game, its for a site. I'm needing a PHP Script to be an active timer, to time how long someone is online, in realtime. Now that i think of it, Javascript/PHP may be better.(Sorry about the first post, it was late @ night.)
#4
http://www.sitepoint.com/forums
They have a massive PHP section and you're almost guaranteed an answer for a question like that within hours or even minutes.
Javascript is clientside. PHP is serverside. Two very different technologies.
Javascript is actually what you want for this. It's too much of a burden on the server to process anything like that in realtime. When the page is exited, you could then send a command back to the server with PHP to then log how long the person was online in total and then store that in a MySQL database.
09/22/2005 (1:52 pm)
You should post questions like that to http://www.sitepoint.com/forums
They have a massive PHP section and you're almost guaranteed an answer for a question like that within hours or even minutes.
Javascript is clientside. PHP is serverside. Two very different technologies.
Javascript is actually what you want for this. It's too much of a burden on the server to process anything like that in realtime. When the page is exited, you could then send a command back to the server with PHP to then log how long the person was online in total and then store that in a MySQL database.
#5
You can find out more about sessions by visiting www.php.net and typing "sessions" in the search box.
Once you have figured out sessions, you can attach a time and a username to it.
Use the PHP command "time();", which creates a UNIX timestamp (the number of seconds passed since the epoch, which is Jan 1 1970). Log that when the session is created in the session variable.
When the session is terminated, or after a certain amount of inactivity, you can log that end time to the database. You can find out the exact time spent online by subtracting the end time by the beginning time. You'll get the # of seconds spent online.
You can then format this by using the PHP "date();" function. You can find out more about how the date function works on www.php.net.
Hope that was a starting point for you. It's a bit difficult to lay something out without knowing exactly what you're doing, and how much knowledge you have of PHP.
09/22/2005 (1:58 pm)
The best way to do this, Chris, is to use sessions.You can find out more about sessions by visiting www.php.net and typing "sessions" in the search box.
Once you have figured out sessions, you can attach a time and a username to it.
Use the PHP command "time();", which creates a UNIX timestamp (the number of seconds passed since the epoch, which is Jan 1 1970). Log that when the session is created in the session variable.
When the session is terminated, or after a certain amount of inactivity, you can log that end time to the database. You can find out the exact time spent online by subtracting the end time by the beginning time. You'll get the # of seconds spent online.
You can then format this by using the PHP "date();" function. You can find out more about how the date function works on www.php.net.
Hope that was a starting point for you. It's a bit difficult to lay something out without knowing exactly what you're doing, and how much knowledge you have of PHP.
#6
09/22/2005 (5:01 pm)
Sam, well the thing is i want the timer to count in realtime, and allow the user to see it. I suppost Javascript/PHP would work. Have Javascript display the time and update it every second, or so, then on window close, send a command to PHP to log it, or when the user clicks a stop button, to log that.
#7
09/22/2005 (5:58 pm)
^ Yes, Javascript is the only way to do that.
Associate Sam Bacsa
What do you need this for?
Calling a PHP script every second would be absolutely killer on the processor and web server, unless you're calling php.exe (or just the PHP executable if you're running *nix) directly. Even then, PHP is a big resource hog to keep calling every second just to update a timer.
EDIT - I can help you better if you explain exactly what you're trying to accomplish.