Schedule the future publishing of items?

I’m developing a small PHP CMS and I want to be able to schedule news items and the like for future dates and automatically post them when that date is due. Would I just include a check script in my header whenever a page is loaded to check for scheduled posts in the database and set them to publish now?

That’s the only way I can think of doing it but that seems like it would be somewhat resource intensive.

Or just store a Publish Date in your database, and have your news page query for entries WHERE publish_date < CURRENT_DATE …

Or store a series of events in an array with post_id in the database and have it check that one entry instead?

So you’re suggesting:

Select Array-Object from Database
Extract Array-Object into Array
Foreach Array as Item
If Item is date-valid
Update news table (or insert)
endif
endforeach
Select news items

is more efficient than

Select news items with WHERE clause

… ok. I beg to differ, but do things however you want.

QFT. This is by far the easiest and best way to solve this.

Well when you say it like that…

Wouldn’t having to search through all the posts each load be inefficient though? I can’t claim to understand how PHP and SQL loads work. I guess you’d have to keep a timer somewhere to prevent the process running more than once a minute.

Well the Database guys could weigh in on this a lot better than I can, but… no. Inefficient would be 4 (or 5) queries instead of 1. (SELECT, UPDATE or [INSERT,DELETE], SELECT, UPDATE vs SELECT). The ‘extra load’ of the database engine having to do a date-based result-restriction would not equate to the processing calls for multiple queries…

@r937? @guido2004? Beuller?

Right OK, that makes sense.

It seems like a normal query to me. And certainly more efficient than the array solution.
If you’re expecting so much traffic that it might become too big a burden on your server then you might want to look into a catching system.

Or, you could just set up a MySQL event (similar to a cron) to do it. You wouldn’t even need to use PHP. Just set the event to run once a day and change published = 0 to published = 1 for articles matching the timestamp specifications.

See: http://dev.mysql.com/doc/refman/5.1/en/create-event.html