So I have a particular field in my database table which I would like to reset to 0 after a fixed period of time, like e.g 1 week. MY language is php. Is there any way to do it? Please help me Okay, so basically I have a column "activation points". These points increase whenever a user does some sort of activity. But I need to reset the points to 0 every week
There is no really nice way to do this with PHP.
You have a few options:
Use MySQL's newly added, built-in, scheduler: Event Scheduler.
Use software from sources other than PHP or MySQL, like cron jobs.
Use a PHP library like PHPScheduler which isn't a true scheduler.
Option 1 is probably your best bet. It is built into MySQL, and you don't have to worry about messing with any other software that you aren't already using.
Option 2 is easy to implement, but does involve using another tool other than just PHP and MySQL. You can learn how to set up cron jobs with this post.
Option 3 is not recommended by me, unless you just absolutely want to use PHP to do this. But you will be limited on the customization of the scheduling. PHPScheduler isn't technically a true scheduler, and you can read why in this post. That post linked is older though, so it will mention that there is no scheduling available in MySQL, but since that post was made, Event Scheduler has been made.
To use Event Scheduler
(Option 1), you should refer to this really really good tutorial and if you have any problems, this documentation.
Here's a preview of how easy it is to use Event Scheduler:
CREATE EVENT nazzus_cool_event
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
INSERT INTO messages(message,created_at)
VALUES('Nazzus cool event just happened again!',NOW());
This code will create an event
called nazzus_cool_event
, and it will occur every 1 minute(s) starting at CURRENT_TIMESTAMP
(now), and ending an hour from now. It will insert some data into the messages table.
It's very simple but you should definitely have a quick look at this great tutorial so that you can see some more of its great features.
You can write a simple mysql statement that will updated your field to 0 and put it on cronjob that will run every week.
edit:
Make sql statement that will reset your column, put it in php file and call it on cronjob, you can check it here , you can always use some free online solutions for cronjob like this one