简体   繁体   中英

When do I need to use cron?

Lets say there is a thread ( on forums ) which will be active for 3 days only. Now, after 3 days, I want this thread automatically closed.

Can I use reference to time when this thread is created in database, and than make if statement if current date + days is bigger than date created, I will print out "<h2>Thread Closed for posting</h2>"

And when I consider some other tasks, I suppose I can use reference to time and have certain event executed on this.

Am I right?

You can use a cron (ran every minute) to set a status field on the thread table to closed such as.

UPDATE threads
SET status='closed'
WHERE lastPost+INTERVAL 3 DAY<NOW()

Then in PHP something such as

if($thread['status'] == 'closed')
{
   // Put your HTML here.
}

A 'cron' is a task that runs at a specific interval or time. This means that it should be used for tasks that must be done without user interaction. For example, a backup, automated emails or pulling data from a remote service.

What you want is better suited to a condition on checking whether a thread is closed. Rather than just having a flag, you also check the age. This means you can change your old-thread logic without needing to edit the database.

You could make a PHP script that gets executed by cron (read up on how to execute PHP in the command line) that SELECTs all the posts in a certain date and then sets them to closed. If you ran that, say, twice a day, then you could do a good job in getting all those posts closed.

Good reference on using cron to run PHP

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