Jump to content

Calculating with times


Ryflex

Recommended Posts

Hi everyone,

I'm trying to add up the current time with a specified amount of seconds and insert it in a database afterwards.

When I add say: 500 seconds to the current time it adds a whole lot more and I get a number few thousands higher than I want. Can anyone help me???.

Gr Ryflex

Link to comment
Share on other sites

$time = time()+7200;

$time_new = $time + $level_up_time + 7200;

              mysql_query("UPDATE buildings

                            SET finish = '$time_new'

                            WHERE member_id = '$id' AND type = '$building'");

column type varchar(25)

thnx

Link to comment
Share on other sites

Why do you add 7200 to the current time twice? You're adding 4 hours to the current time on top of however many seconds $level_up_time is.

 

If you want to just add 500 seconds to the current time, just do:

$time_new = time() + 500;

 

Or if $level_up_time is 500...

$time_new = time() + $level_up_time;

Link to comment
Share on other sites

this is the full code

 

<?php

    require_once('auth.php');
    require_once('config.php');



//               Declaraties
$time = time()+7200;
$building = "Command Center";
$id = 1;//$_SESSION['SESS_MEMBER_ID'];
$global_dbh = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
              or die("Could not connect to database");
              mysql_select_db(DB_DATABASE, $global_dbh)
              or die("Could not select database");



//               Data SELECT
$query_string = "SELECT *
                 FROM buildings
                 WHERE type = '$building' AND member_id = '$id'";
$resultID =  mysql_query($query_string);
$string_arr = mysql_fetch_array($resultID);
$level = $string_arr['level'];
$upgrading = $string_arr['upgrading'];
$finish = $string_arr['finish'];

$food_query = "SELECT *
               FROM members
               WHERE member_id ='$id'";
$food_ID =  mysql_query($food_query);
$food = mysql_fetch_array($food_ID);
$food_amount = $food['food'];

$level = $level + 1;

$level_up_query = "SELECT *
                   FROM upgrade
                   WHERE type = '$building' AND level = '$level'";
$result_level_up = mysql_query($level_up_query);
$level_up_array = mysql_fetch_array($result_level_up);
$level_up_cost = $level_up_array['cost'];
$level_up_time = $level_up_array['time'];




//               Data CHECK
if($upgrading == '0')
{
     if($level_up_cost < $food_amount)
     {
          if($level < '21')
          {



//               Data UPDATE
               mysql_query("UPDATE buildings
                            SET level = '$level+1'
                            WHERE member_id = '$id' AND type = '$building'");
               mysql_query("UPDATE buildings
                            SET upgrade = '$level_up_time'
                            WHERE member_id =$id AND type = '$building'");
               $time_new = $time + $level_up_time + 7200;
               mysql_query("UPDATE buildings
                            SET finish = '$time_new'
                            WHERE member_id = '$id' AND type = '$building'");
               mysql_query("UPDATE buildings
                            SET upgrading = '1'
                            WHERE member_id = '$id' AND type = '$building'");
               $food_new = $food_amount - $level_up_cost;
               mysql_query("UPDATE members
                            SET food = '$food_new'
                            WHERE member_id = '$id'");
               mysql_query("UPDATE members
                            SET timestamp = '$time'
                            WHERE member_id = '$id'");
               header("location: http://ryflex.nl/ryflex/command_center.php");
          }
          else
          {
               echo "If you level up now the level will exceed the level limit.";
          }
     }
     else
     {
          echo "You haven't got enough food to perform this action.";
     }
}
else
{    $time = $time ;//-7200;
     echo "Another building is currently being upgraded. Please wait untill it's finished. <BR>tijd   $time + $level_up_time = $finish";
}
?>
<html>
<head>
<title>Main-Frame</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<center><h1>Command Center</h1></center>
<table width="500" border="1" align="center" cellpadding="2" cellspacing="0">
<tr>
<td><b>Building</b></td>
<td><b>Level</b></td>
<td><b>Upgrade</b></td>
</tr>
<tr>
<td><a href="command_center.php">Command Center</a></td>
<td><?php echo "$command_center_level";?></td>
<td><a href="command_center_exec.php">Upgrade!</a></td>
</tr>
</table>
</body>

</html>

Link to comment
Share on other sites

thnx i found out what the problem was...

the second addition of 7200 seconds. I forgot it was already done in the beginning.

$time = time() +7200

$time_new = (time()+7200) + $level_up_time + 7200

and then $time compared to $time_new is $level_up_time +7200

like DUH....

I currently feel like a dumbass caveman....

thanx for letting me see the light Hypnos

Link to comment
Share on other sites

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.