Maybe a real-life example would help. A few years ago, I worked on an event calendar project using the Zend Framework. In this calendar, a user could create an event and attach 1 or more dates to the event. Naturally, I implemented this as a many-to-one join in my database, but this meant that in order to attach a date, the event had to exist first. However I wanted to allow users to add dates while they were creating the event, not after they created an event. In other words, I wanted the user to edit all aspects of an event at the same time, and submit only when they clicked "save."
I solved the problem by inserting a new empty event record into the database when the user begins creating an event record. This empty record gets filled in and saved when the user clicks "save", or is deleted when the user clicks "cancel". The trouble occurred when users navigated away without clicking "cancel", and the empty event record was left in the database. Eventually, the database would fill up with these meaningless empty events, and things might get ugly.
I wrote a function called "maintenance()", which, among other things, deleted all unsaved records older than 24 hours. I set up a cron job that ran nightly and executed a command-line php script that ran maintenance().
Other things that you might use a cron job for:
- Send a batch of emails to new users. (every 5 minutes?)
- Update user statistics (every hour?)
- Perform resource-intensive operations when the servers aren't slammed with traffic (Every night at midnight, or once a week on sunday nights?)
- Anything other event that doesn't occur in response to a user request (what Jeff calls "out of band").