Ryflex Posted September 12, 2010 Share Posted September 12, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/213241-calculating-with-times/ Share on other sites More sharing options...
Hypnos Posted September 12, 2010 Share Posted September 12, 2010 Can you provide the database column type and the problematic code? Quote Link to comment https://forums.phpfreaks.com/topic/213241-calculating-with-times/#findComment-1110349 Share on other sites More sharing options...
Ryflex Posted September 12, 2010 Author Share Posted September 12, 2010 $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 Quote Link to comment https://forums.phpfreaks.com/topic/213241-calculating-with-times/#findComment-1110352 Share on other sites More sharing options...
Hypnos Posted September 12, 2010 Share Posted September 12, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/213241-calculating-with-times/#findComment-1110363 Share on other sites More sharing options...
Ryflex Posted September 12, 2010 Author Share Posted September 12, 2010 the 7200 seconds or 4 hours is for my timezone. still if I echo the numbers I do get the right amount but when added up it goes up way to high. Quote Link to comment https://forums.phpfreaks.com/topic/213241-calculating-with-times/#findComment-1110365 Share on other sites More sharing options...
Hypnos Posted September 12, 2010 Share Posted September 12, 2010 Your code should do exactly what you're describing, unless $level_up_time isn't being set correctly. Try echoing $level_up_time right before the addition. Quote Link to comment https://forums.phpfreaks.com/topic/213241-calculating-with-times/#findComment-1110367 Share on other sites More sharing options...
Ryflex Posted September 12, 2010 Author Share Posted September 12, 2010 when I echo $level_up_time it does give me the value asked from the database but after that it just flips out Quote Link to comment https://forums.phpfreaks.com/topic/213241-calculating-with-times/#findComment-1110368 Share on other sites More sharing options...
Hypnos Posted September 12, 2010 Share Posted September 12, 2010 Can you give an example of a value for $level_up_time? Like what is it when you echoed it just now? Quote Link to comment https://forums.phpfreaks.com/topic/213241-calculating-with-times/#findComment-1110374 Share on other sites More sharing options...
Ryflex Posted September 12, 2010 Author Share Posted September 12, 2010 1284338201 + 500 = 1284346000 $time + $level_up_time = $finish refreshed it 2 sec ago got this Quote Link to comment https://forums.phpfreaks.com/topic/213241-calculating-with-times/#findComment-1110375 Share on other sites More sharing options...
Hypnos Posted September 12, 2010 Share Posted September 12, 2010 That doesn't add up... One of those variables must be incorrectly set. Can you post more code? Quote Link to comment https://forums.phpfreaks.com/topic/213241-calculating-with-times/#findComment-1110378 Share on other sites More sharing options...
Ryflex Posted September 12, 2010 Author Share Posted September 12, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/213241-calculating-with-times/#findComment-1110384 Share on other sites More sharing options...
Hypnos Posted September 12, 2010 Share Posted September 12, 2010 Your code looks right, other than your $time + $level_up_time = $finish. $finish is being pulled from the database, but $time isn't. That's why it doesn't add up. The difference will be whatever seconds have passed since the last time it was loaded. Quote Link to comment https://forums.phpfreaks.com/topic/213241-calculating-with-times/#findComment-1110386 Share on other sites More sharing options...
Ryflex Posted September 12, 2010 Author Share Posted September 12, 2010 ok but then the difference should be smaller right..... the $finish comes from the database while $time is just the current time..... so if the difference became smaller i would understand but its bigger.... Quote Link to comment https://forums.phpfreaks.com/topic/213241-calculating-with-times/#findComment-1110388 Share on other sites More sharing options...
Hypnos Posted September 12, 2010 Share Posted September 12, 2010 Well, there's another problem with that check. You're using $time. $time is 2 hours ahead. You don't add the other 2 hours until you set $time_new. You should add in all 4 hours at once, instead of 2 hours at a time. Quote Link to comment https://forums.phpfreaks.com/topic/213241-calculating-with-times/#findComment-1110390 Share on other sites More sharing options...
Ryflex Posted September 12, 2010 Author Share Posted September 12, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/213241-calculating-with-times/#findComment-1110394 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.