Page 1 of 1
working with time
Posted: Mon Nov 28, 2005 10:06 am
by nawhaley
Hi everyone I'm looking for a way to use PhP to do time comparisons. I need it to check the current time vs a set time of 30 minutes from the time the user clicks a specific link or button. Basically it should work kind of like the following.
1)user clicks link or button timer started 30 min
2) Php periodically checks set time against current server time
3) time reached user is redirected to timeout page.
any ideas how to execute this at all?
Posted: Mon Nov 28, 2005 10:08 am
by Burrito
1) use sessions
2) stamp a time when the button is clicked and store it in a session var
3) compare the current time with the stamped session time
4) when 30 mins is up, do whtever you want to do.
Posted: Mon Nov 28, 2005 10:14 am
by nawhaley
ok I understand I would need to use a session variable to accomplish this but can you define alittle more clearly how to "stamp" up on clicking. Also I've tried time comparisons and apparently I'm doing it wrong I would do something similar to this script
Code: Select all
$settime = mktime(0,30,0,0,0,0);
$currenttime = time();
$diff = settime - currenttime;
I set this up as a tes basically it uses mktime to set a time value of 30 minutes ahead of the current time basically making a "timestamp" like you suggested and then compares it to the current time but this always returns 0 what am I missing?
Posted: Mon Nov 28, 2005 10:26 am
by Burrito
stamp your time upon the button click:
Code: Select all
if(isset($_POST['buttonwhatever']))
$_SESSION['timestamp'] = time();
then compare the current time to that timestamp:
Code: Select all
$curtime = time();
$difftime = $curtime - $_SESSION['timestamp'];
echo $difftime;
Error
Posted: Mon Nov 28, 2005 2:32 pm
by nawhaley
hmm it seems PhP is making some assumptions when it runs this code heres what the error log is reading off to me.
Code: Select all
[client 127.0.0.1] PHP Notice: Use of undefined constant curtime - assumed 'curtime' in C:\\Web\\livequiz2.php on line 55, referer: http://localhost/livequiz2.php
[client 127.0.0.1] PHP Notice: Use of undefined constant timepast - assumed 'timepast' in C:\\Web\\livequiz2.php on line 56, referer: http://localhost/livequiz2.php
here is the code
Code: Select all
$curtime = time();
$timepast = curtime - $_SESSION['timestamp'];
$timepast = timepast/60;
$timeleft = 30-$timepast;
print('<P Align = "right"> Estimated Time Left ');
print $timeleft;
print(" minutes </P>");
writequestion();
print("<BR>");
$_SESSION['timestamp'] was initalized earlier as time().
Any ideas why when the page refreshes its not updating the timepast and the timeleft variables?
Posted: Mon Nov 28, 2005 2:36 pm
by Burrito
put a "$" in front of your variable names.
Posted: Mon Nov 28, 2005 2:41 pm
by nawhaley
Oy vey...I apologize for the stupidity of the previous question why I didnt notice it I dont know ..mayhaps I need more sleep :/.
Posted: Mon Nov 28, 2005 2:44 pm
by foobar
nawhaley wrote:Oy vey...I apologize for the stupidity of the previous question why I didnt notice it I dont know ..mayhaps I need more sleep :/.
Don't worry, happens to the best. I code in PHP for a project that I'm doing and Java for uni, interchangably. Sometimes I write $ infront of variable names in Java or declare variables in PHP (eg: int banana;).
