Search code examples
phpmysqlarticle

Delete a post after a period of time


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.


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);