1

I'm doing a school project in which I have to put news in an SQL database and, when a certain time passes (around a week or so) automatically edit the database to notify that a specific piece of news is no more active. For example, I put on a news on monday, and I want it to automatically disappear (or appear as "closed") on the morning after. So, what I would need is a PHP function (or an SQL one) that automatically starts with a specific timeout and edit the database. Does anyone know something like that? Thank you in advance for your help

PS I'm using PHP 5.3.10 and SQL 3.4.10 and I'm stick to them because they're installed on my lab computers

1
  • You don't really need to update it, since you have the date in your columns, you can just select values where date = or higher than today, so you will not select the values with older dates. Commented Jan 27, 2014 at 10:27

1 Answer 1

2

I don't think so. You would use a cron script or something similar to do this on a timer - but this is the wrong approach anyway.

What you need to do is add a time constraint to your SQL. So that your SQL "gets all the news articles for the last 7 days" or whatever

You can do this as follows:

SELECT * FROM news WHERE date > DATE_SUB(NOW(), INTERVAL 1 WEEK)

That way every time a user goes to your page, they will get all the records within a week of the current time. And you don't have to delete anything.

Remember the PHP and SQL query are only run when the website is refreshed. Hope that makes sense.

Sign up to request clarification or add additional context in comments.

1 Comment

that seems legit. Thank you very much :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.