Jump to content

Recommended Posts

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"

}

?>

Link to comment
https://forums.phpfreaks.com/topic/175405-solved-php-time-comparisons/
Share on other sites

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.