working with time

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

Post Reply
nawhaley
Forum Commoner
Posts: 85
Joined: Wed May 18, 2005 11:43 am

working with time

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

Post 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.
nawhaley
Forum Commoner
Posts: 85
Joined: Wed May 18, 2005 11:43 am

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

Post 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;
nawhaley
Forum Commoner
Posts: 85
Joined: Wed May 18, 2005 11:43 am

Error

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

Post by Burrito »

put a "$" in front of your variable names.
nawhaley
Forum Commoner
Posts: 85
Joined: Wed May 18, 2005 11:43 am

Post 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 :/.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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;). :roll:
Post Reply