/    Sign up×
Community /Pin to ProfileBookmark

PHP and Cron jobs

I’ve been working on writing a text based browser game in PHP. I want the game to play in rounds that are one week long.

So, once a week I want to purge the variables in the database and start over for a new round of play.

I believe the best way to accomplish this is with a setting up a cron job to execute once a week. Am I correct, would this be the best way to accomplish this?

I’ve never set up any cron jobs before, so this next question might sound silly: I can write a cron job in PHP, right?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@chrisranjanaJun 22.2012 — do you want some actions to happen to ALL USERS every week ? then cron is good.

But if you want to check whether 1 week has passed from the LAST LOGIN date of a particular user before doing an action then more complicated LOGIC is involved !
Copy linkTweet thisAlerts:
@The_Little_GuyJun 22.2012 — You can write a cron job in any language you wish, as long as the server can run/understand the file.

purging the database contents isn't always a good idea, so you may want to think whether you want to do that or not, I think I would need more information before giving you a solid answer on that though.
Copy linkTweet thisAlerts:
@chrisranjanaJun 22.2012 — You can write a cron job in any language you wish, as long as the server can run/understand the file.

[/QUOTE]


[sugnosaj]I've been working on writing a text based browser game in PHP [/sugnosaj]
Copy linkTweet thisAlerts:
@sugnosajauthorJun 22.2012 — Firstly, I must concede that what i'm trying to accomplish is currently outside of my abilities. But, hey, thats how you learn.

I want to either export the contents of the database to an xml file, or create a new table to work with and just import a few things from the old table.

I also want to be able to run a script that will count down to the next time the cron job is executed.
Copy linkTweet thisAlerts:
@sugnosajauthorJun 22.2012 — I think I established that a cron job is in fact the best way to accomplish what I want. And I think for my current skillset, PHP will be the best for me to use.

Now, the part I'm really clueless on, is how can I run a script that countdowns to the next time the cron job is executed? My site is hosted on godaddy's server, if that helps. I think this is getting into more server admin kind of stuff that I don't know.
Copy linkTweet thisAlerts:
@sugnosajauthorJun 22.2012 — I think what i'm really trying to ask here is how to access the server's clock.
Copy linkTweet thisAlerts:
@The_Little_GuyJun 22.2012 — Do you really want to do that?

Remember Databases can hold TONS of data and still run at lightning speed. So you can then just search for information between certain dates, for example:

<i>
</i>select sum(transfer_amount) as balance from bank where date between '2012-06-17' and '2012-06-23 23:59:59' and member_id = 123;
Copy linkTweet thisAlerts:
@sugnosajauthorJun 22.2012 — You guys seem real hesitant about purging the database. Once the game round ends, it resets. Those variables are no longer useful, no longer accessible to the player. I want to preserve that data for other uses by exporting it to an xml. But I really can't think of any reason to preserve that data in the database.

Maybe I just did a poor job of communicating my objective. I'm a big fan of themafiaboss.com a text based brower game that plays in different rounds. I'm trying to make something along those lines.
Copy linkTweet thisAlerts:
@bsmbahamasJun 22.2012 — i could never really wrap my head around cron jobs, i really did try hard though, lol. what i ended up doing is just creating a simple script that checked the date, and compared it to a date stored in a text file on my server.

i had a fair amount of traffic, so all my script did was check to see if the date in the file had changed and if so it did all the stuff i wanted the chron to do. this was simply tacked onto my login page and index page, so once someone dropped by and the date had changed it fired off, and then updated the date to the current date, so it would still check each time someone came by but only fire off if the date changed - i used it to email weekly stats to all my users. another version of this script changed all my prices to discounted prices on the last weekend of each month and converted it back after the weekend was up.

in short it reads the file, let's call it last-update.php and if the date in there was not the same as today's date it ran the script and updated the date inside the file so it would only run once per day.

you could easily have it check by hour or by month as well, and ofcourse you can store the last update inside your mysql database.

there are also several online sites that let you setup an external chron job that point to a php file on your server and it loads that file for you just like how my script works as-if someone loaded the page in their browser.
×

Success!

Help @sugnosaj spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...