cloudll Posted January 25, 2013 Share Posted January 25, 2013 Hi guys. I'm trying to think of a way to increase the number in a database over time. Its for a resourse section for a game im creating. Similar to lords and knights if anyone has played that on their phones. At the moment the game refreshes every 30 seconds so I can easily code it to add +1 to the database on every refresh. But I have no idea how I would make it so the +1 was added say every minute, even when the user was logged out. I was wondering if someone could suggest some reading material or some function to look up that may help me with this. I have tried google but not had much luck. Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/273650-adding-numbers-to-a-database-over-time/ Share on other sites More sharing options...
Jessica Posted January 25, 2013 Share Posted January 25, 2013 Cron job. Quote Link to comment https://forums.phpfreaks.com/topic/273650-adding-numbers-to-a-database-over-time/#findComment-1408286 Share on other sites More sharing options...
cloudll Posted January 25, 2013 Author Share Posted January 25, 2013 Lovely, thanks. I have done a quick google on cron jobs. So If Im understanding it correctly, I would have a simple bit of code to add+1 to the database, then I would set up a cron job in my hosting control panel to execute the code every minute. Is that correct ? Quote Link to comment https://forums.phpfreaks.com/topic/273650-adding-numbers-to-a-database-over-time/#findComment-1408288 Share on other sites More sharing options...
gevo12321 Posted January 25, 2013 Share Posted January 25, 2013 why dont you keep track of when they log out/log in and add it all in when they log in Quote Link to comment https://forums.phpfreaks.com/topic/273650-adding-numbers-to-a-database-over-time/#findComment-1408291 Share on other sites More sharing options...
cloudll Posted January 25, 2013 Author Share Posted January 25, 2013 Yes that could be possible. The cron job, if it works like i think, may be easier for me, as I am not too sure how i would work out the difference between the log out and log in, and add it to the database. Maths was never my strong point Quote Link to comment https://forums.phpfreaks.com/topic/273650-adding-numbers-to-a-database-over-time/#findComment-1408293 Share on other sites More sharing options...
Barand Posted January 26, 2013 Share Posted January 26, 2013 As you still want to increment even when they are not logged in then what you need to know is how many minutes have elapsed since they originally started playing the game. There is, therefore, no necessity to continually update the database every minute with the resource overhead that incurs. <?php $gameStarted = '2013-01-22 18:00:00'; $minutes = minutesSince($gameStarted); echo "Game started at $gameStarted<br>"; echo "Current time is " . date('Y-m-d H:i:00') .'<br>'; echo "Minutes since start = $minutes"; function minutesSince($dt) { $start = new datetime($dt); $diffs = $start->diff(new datetime)->format('%a:%h:%i'); list($d, $h, $m) = explode(':', $diffs); return $d*24*60 + $h*60 + $m; } ?> RESULTS Game started at 2013-01-22 18:00:00 Current time is 2013-01-26 13:16:00 Minutes since start = 5476 Quote Link to comment https://forums.phpfreaks.com/topic/273650-adding-numbers-to-a-database-over-time/#findComment-1408365 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.