Page 1 of 1

Timer or Timeout??? How cam I do that?

Posted: Mon Jul 07, 2003 11:21 pm
by alexus
How can I do timer in PHP (meaning that after time is up, it will execute some function)

8O PS: In java I did this as:

Code: Select all

setTimeout('time_out()','120000')
function time_out(){
<?
	//Setting user status to OffLine
	$db = mysql_connect("localhost","root", "");
	mysql_select_db("test", $db);
	$sql = "UPDATE users SET status='OffLine' where u_name='$u_name'";
	mysql_query($sql);
?>
//window.document.write("<b>OffLine</b>");
&#125;
PS2: Yes, that JavaScript + PHP

Posted: Tue Jul 08, 2003 12:56 am
by Drachlen
What I'm using for my game to time building (ie: the player starts constructing a building, it sets a timer, when its done it builds it) is using a few variables that get:
1. The current time
2. the done time (current time+something)

What you will need to do is make an if statement checking if the link or whatever has been clicked, and if it has, then write the current time into 1 field, then the finish time into another. Then check if the current time is equal to or greater than the current time... The only way to execute it though is by refreshing..

Heres an example, it needs alot of modding if you want it per user, but it works for just testing..

Code: Select all

<?php

$time = date(i);
$time1 = $time+2;
    $link = mysql_connect("localhost", "***", "***") 
        or die("Could not connect"); 
    mysql_select_db("your_database", $link) or die("Could not select database"); 
    $query = MYSQL_QUERY("SELECT * FROM time"); 
    while ($fetch=mysql_fetch_array($query)) {
$cur = $fetch[current];
$sta = $fetch[stamp];

echo "Time it was clicked: $fetch[stamp]<br>
	Time it is done: $fetch[current]<br>
	Current time: $time<br>";
}
if($cur == $time or $time > $cur) {
   $aa = "DELETE FROM time";
   $bb = mysql_query($aa);
echo "Done<br>";
} else{
$aa4 = $cur-$time . " minute(s) left.";
echo $aa4."<br>";
}

echo "<a href=aa.php?up=yes>GGOGOGOGO</A>";
if($up == "yes") {
mysql_query("INSERT INTO time (tid, stamp, current) VALUES ('', '$time', '$time1')", $link) or die("Could not select database");
}
?>
Just keep in mind you would need some more fields like username, and its not really structured in the way you may want, just an example... if you want to test it set up a table called time with the fields tid, stamp, current and make sure tid is auto increment..

Posted: Tue Jul 08, 2003 10:49 am
by alexus
Yes, but I can’t refresh the page …. That’s why I used java to call php function so it will be executed, but it doesn’t work when I’m putting my code to the main page….

So I need something that will update my DB after some time without refreshing the page…

PS: I guess I have to go to Java forum… or VB