How can I update MySQL table value on Future Date?
I'm just looking for some general direction on this one. Basically I have a date set as one of the parameters of a mysql database. The date is set for a date in the future from when the value was set. When the future date is reached I would like to update a certain table value. How do I go about doing this?
Also, as a related question, how can I set a script to ch开发者_StackOverflow社区eck the date and update the table automatically? Can I use a PHP script or do I have to set up some complex cron job to run daily?
You answered the first question with the second question. You use a script that checks the dates and updates the table.
Yes, you need to use cron, but it does not need to be complex. Cron can run a php script (in cli mode). You can also have cron start curl
or wget
and have them run the php directly on the webserver.
Well, actually you have one other option - every time you do some sort of action on your site (you will need to decide which action is most appropriate) have it also run the update commands.
When the future date is reached I would like to update a certain table value
Why not just write your code to select the appropriate value based on the date, e.g.
SELECT IF(activation_date<NOW(), old_value, new_value) AS value
FROM yourTable
WHERE...
For both the situation you would need to setup a cron job(not necessarily complex though). Depending on the application, you may decide to do the update whenever some sort of request is made. For example, say a future data is validity of bonus amount and whenever, a particular user logs in, you would make the check and invalidate the bonus amount if the date is reached. If it's really necessary to have the change instantaneously, cron jobs are the way to go.
It all depends on the Application and it's scope.
精彩评论