markvaughn2006 Posted September 24, 2009 Share Posted September 24, 2009 Think this would work for my game? (Guy gets attacked and sent to hospital for 5 minutes) <?php $time=time(); //Setting variable to current time plus 5 minutes $timeinhospital == $time+ 300; //update db $result = mysql_query("UPDATE example SET time='$timeinhospital'") or die(mysql_error()); ?> <?php if ( $timeinhospital < $time ) { echo "Ok you are healed or whatever Leave the hospital now!<br />"; } else { echo "You are still in hospital" } ?> Quote Link to comment https://forums.phpfreaks.com/topic/175405-solved-php-time-comparisons/ Share on other sites More sharing options...
Alex Posted September 24, 2009 Share Posted September 24, 2009 Pretty much.. Just a few things. The first snippet you posted would be ran only when they're sent, and you need to fix this line on it: $timeinhospital == $time+ 300; Should be: $timeinhospital = time() + 300; //There's no need to define $time Remember, '==' is for comparing only. And because the second snippet is separate from the first you need to define $time (or just use time() in the conditional) and $timeinhospital. Quote Link to comment https://forums.phpfreaks.com/topic/175405-solved-php-time-comparisons/#findComment-924309 Share on other sites More sharing options...
mikesta707 Posted September 24, 2009 Share Posted September 24, 2009 As long as the snippets are on the same page, you don't need to redefine the variables again, they are usable throughout the entire script (well relative to their scope of course, you can't use them in functions and what not) also another thing, since you are adding 300 to the $time variable, and setting that as teh $timeinhopital variable, the following if statement if ( $timeinhospital < $time ) { will always be true, assuming those snippets are on the same page. If they aren't, don't worry about the latter, but you will have to take what AlexWD said into account about the redefinitions of $time and $timeinhospital Quote Link to comment https://forums.phpfreaks.com/topic/175405-solved-php-time-comparisons/#findComment-924316 Share on other sites More sharing options...
markvaughn2006 Posted September 24, 2009 Author Share Posted September 24, 2009 cool thanks guys!! i didn't realize the difference between = and == really either, now i know Quote Link to comment https://forums.phpfreaks.com/topic/175405-solved-php-time-comparisons/#findComment-924384 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.