Question

I have created a function that gets the time an article was published. The same function checks whether the article is 1 week old and deactivates it.

I don't know what to do here if I have to make the function responsive when I call. I thought of using JavaScript and setting a timeout for the function. that would be too much of looping.

This is the "if" that I used to check the date. Someone with a better idea, please help.

if((time() - $publish_time) > (7*24*60*60)){
  //  if the current timestamp - the time the article was published > 1 week.
  //Execute the query to deactivate the article
} 

That snippet is inside a function that I have to call.

Was it helpful?

Solution

Imagine that [table] has [column] with the date of the post got inserted:

SET GLOBAL event_scheduler = ON;

CREATE
    EVENT 'archive_posts'
    ON SCHEDULE EVERY 1 WEEK
    DO 
        DELETE FROM [table] WHERE [column] < DATE_SUB(NOW(), INTERVAL 1 WEEK);

OTHER TIPS

Rather than having this inside your site, you should expire your articles with a cronjob, as per this question when using MySQL (*nix-like)
On Windows, you can use mmc.exe and add a Task to execute the script through Task Scheduler)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top