Jump to content

Making A Time Based Calculation


Usarjjaco

Recommended Posts

Hello Everyone,I'm sure there's a pretty simple answer to this. I'm doing PHP with a MySQL database to run this. Basically it's a program that logs someone in, and credits them with X amount of something per minute they are logged in. Once they Log Out (This isn't like a user log in/log out, it's a form) the amount is calculated.So if they earned 5 points per minute and they logged in at 2100, logged out at 2200.... How would I get it to calculate they were on for 60 minutes and therefore 60 * 5 = accrued points.Thanks I know I'm missing something that's right in front of me.

Link to comment
Share on other sites

I just spent an evening with similar questions. These pages answered my questions: http://dev.mysql.com/doc/refman/5.1/en/dat...l#function_timeand to a lesser extent: http://www.w3schools.com/sql/sql_dates.asp

Link to comment
Share on other sites

I'm a bit confused. How are you going to ensure the consistency of "logged" in users if you aren't storing their information anywhere? User A could sign in with different names each time (even accidentally), and thus never accumulate a cumulative point count. If that's not an issue, then you could use $_SESSION to track each users time by using time stamps and calculate the difference from the start of the session to the end.

Link to comment
Share on other sites

I'm a bit confused. How are you going to ensure the consistency of "logged" in users if you aren't storing their information anywhere? User A could sign in with different names each time (even accidentally), and thus never accumulate a cumulative point count. If that's not an issue, then you could use $_SESSION to track each users time by using time stamps and calculate the difference from the start of the session to the end.
I'm going to read those two links. The users are already logged into a user table in the database before they use the form. (It's a form for a game where they are logging when they sign in and out of the game). So I already have the user being consistant, and the idea is that when they log in it starts to calculate the time they accrue until they submit that they are logged out.EDIT: Also the point count isn't going to be cumulative. It's on a per "operation" basis. Each different operation will have a row in a table for that user.
Link to comment
Share on other sites

<?php$m1=mysql_query ("SELECT * FROM miners WHERE username='$uname'");$m2=mysql_fetch_assoc($m1);$m3m=$m2['mm3m'];$curtime=time()$oper=$_POST['opname'];if ($_POST['log']== logon) {$blazzy=("INSERT INTO miningops (opname, miner, m3m, ontime)VALUES ('$oper', '$uname', '$m3m', '$curtime')");echo $curtime;echo "<br>";echo $m3m;echo "<br>";echo $uname;echo "<br>";echo $oper;}

Ok, the above code is where if the user selects they are beginning the operation, it should insert a new row. (The Echo's were to make sure the information was getting sent correctly, which all of it shows up properly, but it does not create a new row in the table... I am sure I'm missing something silly here)Now below is the overall code, I want to make sure it looks right to you guys (It's kinda rough right now but I'm just making sure my concept is right)

<?php$m1=mysql_query ("SELECT * FROM miners WHERE username='$uname'");$m2=mysql_fetch_assoc($m1);$m3m=$m2['mm3m'];$curtime=time()$oper=$_POST['opname'];if ($_POST['log']== logon) {$blazzy=("INSERT INTO miningops (opname, miner, m3m, ontime)VALUES ('$oper', '$uname', '$m3m', '$curtime')");echo $curtime;echo "<br>";echo $m3m;echo "<br>";echo $uname;echo "<br>";echo $oper;}else {$stime=mysql_query ("SELECT * FROM miningops WHERE username='$uname' AND opname='$_POST[opname]'");$startime=mysql_fetch_assoc($stime);$starttime=$startime['ontime'];$amount=$startime['m3mm'];$diffy=TIMEDIFF('$curtime', '$starttime');$secondsa=TIME_TO_SEC ('$diffy');$difference=$secondsa / 60;$ouput = $difference * $amount; $sql2=("INSERT INTO miningops WHERE username='$uname' AND opname='$POST[opname]' (offtime, difference, output)VALUES ('$curtime', $difference, $output)");}?>

Basically The Code States This:If a user selects they are logging in it:1) Creates a new operation row (Which isn't working right now as per my upper code post)2) Inserts information in the columns opname, miner, ontime.If a user selects they are logging off it:1) Takes the start time, and subtracts it from the end time2) Converts the difference into seconds3) Divides the seconds by 60 to make it minutes4) * the number of minutes by the amount per minute5) Inserts this info into the offtime, difference, and output columns of the row.So in summation: What is wrong with my upper code post, and does my lower code post look like it will work properly?

Link to comment
Share on other sites

in the upper code I don't see where you are establishing a connection with the database.
I do have an established connection. It is pulling thru the m3 amounts and the miner's name and such from the DB. Is there anything else with the code that looks amiss? I have the ontime, offtime, and difference fields set as "time " and not null.EDIT: Found the problem, knew it was silly. Forgot the mysql_query on the insert line ... now to make sure the bottom code works...EDIT 2: Had to make a couple slight changes but working like a charm now. Thanks for the help guys.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...