简体   繁体   中英

php - how to store expiry date in a database and run a (delete) script at date

I have a simple website where users can post ads, i want to set an expiry date based in a mysql table when the user uploads an ad, say in one months time and then when that date is reached then my delete script will be automatically run to remove the ad

any help much appreciated!!

add script:

INSERT INTO ads (expires_on, ...)
VALUES (now() + INTERVAL 1 MONTH, ...) 

delete script:

SELECT ...
WHERE expires_on <= now()

Simply have your delete script run once a day (eg cron job)

  1. Write a script which queries the database and deletes/flags any expired ads based on a timestamp set when the ad is created.
  2. Call this script from a cron job set to run nightly.

Better yet, if the data set is small, ignore the expired ads in your main code instead of deleting them at all.

add query:

INSERT INTO ads (expires_on, ...)
VALUES (now() + INTERVAL 1 MONTH, ...) 

delete query:

DELETE from ads 
WHERE expires_on <= now()

And run them in cron jobs, as others mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM