oliverj777 Posted April 6, 2012 Share Posted April 6, 2012 Hello, I'm working on a simple project so bear with me, I have a variable called 'wood' on my SQL database, and I want to somehow increase the 'wood' value (int) every 5 seconds... Even when the user is not currently view that page. So what I'm thinking is saving the current mktime() to my database once - for sake of argument, this is going to be called 'time_cache', as well as the wood(int) variable. Then, when the user logs in to see how much wood there is, the PHP will, take the mktime() and subtract the 'cache_time' to see how many seconds have passed... Now I want to increase the wood int by 1 every 5 seconds that have passed. So I guess I need to do a division such as $seconds_passed = (mktime() - $time_cache) / 5. Which will give me the amount of 5 seconds that have passed, right? Then, surely my $wood will just equal $seconds_passed? I'm not sure if this is the best way to do something like this? Any one else have an idea? Also, my wood value doesn't 100% work as it give a decimal... I just want the wood value to increase by 1, every 5 seconds. Quote Link to comment https://forums.phpfreaks.com/topic/260448-increase-value-in-database-every-5-seconds/ Share on other sites More sharing options...
freelance84 Posted April 6, 2012 Share Posted April 6, 2012 floor will remove the decimal places in this case. Quote Link to comment https://forums.phpfreaks.com/topic/260448-increase-value-in-database-every-5-seconds/#findComment-1334920 Share on other sites More sharing options...
oliverj777 Posted April 6, 2012 Author Share Posted April 6, 2012 floor will remove the decimal places in this case. Okay, so this works nicely... My wood increases by 1 every 5 seconds, one major floor though (sorry for the pun ), is that my code forces my wood to be fixed, by that I mean that I can't subtract wood due to it being measured in my cached time, and relative time. What I was hoping for, was something like my wood has a default value of, say 20 on my database and my 'time setup' would just add 1 every 5 seconds to that value... Instead, what happening is that my time setup IS my wood value. Any ideas? Would really appreciate it, thanks Quote Link to comment https://forums.phpfreaks.com/topic/260448-increase-value-in-database-every-5-seconds/#findComment-1334925 Share on other sites More sharing options...
Samuz Posted April 6, 2012 Share Posted April 6, 2012 Just use update and add wood to the current time value. something like.. UPDATE `table` SET `time` = time+$wood WHERE `id` = '44' Quote Link to comment https://forums.phpfreaks.com/topic/260448-increase-value-in-database-every-5-seconds/#findComment-1334934 Share on other sites More sharing options...
scootstah Posted April 6, 2012 Share Posted April 6, 2012 My wood increases by 1 every 5 seconds The ladies must love you. *cough* Sorry... Anyway, if you want to run code on specific time intervals without a user executing it (viewing the page) then you'll have to use something like a cron job. Quote Link to comment https://forums.phpfreaks.com/topic/260448-increase-value-in-database-every-5-seconds/#findComment-1334937 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.