简体   繁体   English

什么时候需要使用cron?

[英]When do I need to use cron?

Lets say there is a thread ( on forums ) which will be active for 3 days only. 可以说有一个主题(在论坛上)将仅激活3天。 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>" 我可以在数据库中创建该线程时使用引用时间,如果当前日期+天大于创建日期,则可以使用if语句进行打印,我将打印出"<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. 您可以使用cron(每分钟运行一次)将线程表上的状态字段设置为“关闭”。

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

Then in PHP something such as 然后在PHP中,例如

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

A 'cron' is a task that runs at a specific interval or time. “ cron”是一项在特定间隔或时间运行的任务。 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. 您可以制作一个由cron执行的PHP脚本(请阅读如何在命令行中执行PHP),该脚本选择某个日期的所有帖子,然后将其设置为关闭。 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 有关使用cron运行PHP的良好参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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