I run a text based game and I am having some trouble. I have a hospital section of the game where if you loose a fight you go here for a period of time. Now you see the time is supposed to go down say if you go to the hospital at 12:00 for 45 minutes. 5 minutes later the time left for your stay should be 40 minutes. But this is not being put in to affect. Here is the code for that page:

<?php
/*
MCCodes FREE
hospital.php Rev 1.1.0c
Copyright (C) 2005-2012 Dabomstew

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

session_start();
require "global_func.php";
if ($_SESSION['loggedin'] == 0)
{
    header("Location: login.php");
    exit;
}
$userid = $_SESSION['userid'];
require "header.php";
$h = new headers;
$h->startheaders();
include "mysql.php";
global $c;
$is =
        mysql_query(
                "SELECT u.*,us.* FROM users u LEFT JOIN userstats us ON u.userid=us.userid WHERE u.userid=$userid",
                $c) or die(mysql_error());
$ir = mysql_fetch_array($is);
check_level();
$fm = money_formatter($ir['money']);
$cm = money_formatter($ir['crystals'], '');
$lv = date('F j, Y, g:i a', $ir['laston']);
$h->userdata($ir, $lv, $fm, $cm);
$h->menuarea();
print
        "<h3>Hospital</h3>
<table width='75%' border='2'><tr bgcolor=gray><th>ID</th><th>Name</th <th>Level</th> <th>Time</th><th>Reason</th></tr>";
$q =
        mysql_query(
                "SELECT u.* FROM users u WHERE u.hospital > 0 ORDER BY u.hospital DESC",
                $c);
while ($r = mysql_fetch_array($q))
{
    print
            "\n<tr><td>{$r['userid']}</td><td><a href='viewuser.php?u={$r['userid']}'>{$r['username']}</a> [{$r['userid']}]</td><td>{$r['hospital']} minutes</td><td>{$r['hospreason']}</td></tr>";
}
print "</table>";
$h->endpage();

Any suggestion on what to do or where to look for this problem?

The PHP date is when the server generates a date and sends it to the user for viewing. The only way to do this is by using Javascript:

  1. Get Javascript to refresh the page
  2. Get Javascript to just do the count down (click here)

Feel free to PM me if you need any more help ;)

I am rather new at this where or how would i put it in?

Right no worries ;)

First you put <span id="timer"></span> wherever you want to display the timer

Second you put the following in the <head>

<script type="text/javascript">
var count= 2700; // will count down from 45 mins

var counter=setInterval(timer, 1000);

function timer()
{
  count=count-1;
  if (count <= 0)
  {
     clearInterval(counter);
     return;
  }

 document.getElementById("timer").innerHTML=count + " secs";
}
</script>

You can google it and expand if you want on converting seconds to mins etc

and there you have it :)

i applied this. but the variable it self doesnt go down. like i set the var count to equal the hospital value but the hospital value does not go down

I would suggest timestamping the moment the person is supose to be released from the hospital in your database. Then you would compare the current time with the stored time to determine how much time is left if any. something to this effect: (using your code starting on line 49)

$q = mysql_query("SELECT u.* 
                  FROM users u 
                  WHERE u.hospital<".time()." 
                  ORDER BY u.hospital DESC",$c);
while ($r = mysql_fetch_array($q)){
    print
        "\n<tr><td>{$r['userid']}</td><td><a href='viewuser.php?u={$r['userid']}'>{$r['username']}</a> [{$r['userid']}]</td><td>".($r['hospital']-time())/60." minutes</td><td>{$r['hospreason']}</td></tr>";
}

Note that in order for this to work the methodology of storing the hospital value in your db must change. So wherever the penalty happens you need to specify the release time as x number of seconds added to the current time() for that individual user. By your example you would have something like this

$hospitalReleaseTime=time()+(60*45)//change 45 to the proper penalty minutes.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.