Jump to content

adding numbers to a database over time.


cloudll

Recommended Posts

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

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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

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.